避免 Chip Group 不受检查

Avoid Chip Group to get unchecked

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

<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>

您可以要求具有 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">

请注意:app:checkedChip 或不需要初始选择。如果未设置,则无需选择即可在任何情况下使用。

注意:这需要最低版本 1.2.0