避免芯片组不受检查

Bru*_*ins 6 android android-chips material-components material-components-android

我有以下芯片组,当我点击已经选中的芯片时,它会被取消选中,但我必须始终有一个选中的芯片。行为类似于无线电组。

<com.google.android.material.chip.ChipGroup
        android:id="@+id/chip_group_filter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:checkedChip="@id/chip_1"
        app:singleSelection="true">

        <com.google.android.material.chip.Chip
            android:id="@+id/chip_1"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chip1"
            app:chipBackgroundColor="@color/chip_color" />

        <com.google.android.material.chip.Chip
            android:id="@+id/chip_2"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/chip2"
            app:chipBackgroundColor="@color/chip_color" />

    </com.google.android.material.chip.ChipGroup>
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 11

您可以要求具有该app:selectionRequired属性的选定芯片。
就像是:

 <com.google.android.material.chip.ChipGroup
    android:id="@+id/chip_group_filter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:checkedChip="@id/chip_1"
    app:selectionRequired="true"
    app:singleSelection="true">
Run Code Online (Sandbox Code Playgroud)

请注意:app:checkedChip或者不需要初始选择。如果没有设置它在任何情况下都可以工作而无需选择。

注意:这需要最低版本 1.2.0