【问题标题】:Avoid Chip Group to get unchecked避免芯片组未经检查
【发布时间】:2020-02-03 07:28:58
【问题描述】:

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

<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 需要选定的芯片。

标签: android material-components material-components-android android-chips


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2021-08-04
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多