避免芯片组得到未经检查的情况。

9
我有一个芯片组,当我点击已经选中的芯片时,它会变成未选中状态, 但是我需要始终有一个被选中的芯片。这种行为类似于单选按钮组。
<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>

现在有一个新的API可要求选定的芯片。 - Gabriele Mariotti
1个回答

17

您可以使用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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接