Android:设置默认选择芯片

Haw*_*ike 3 xml android material-design android-chips material-components-android

有没有办法将选择芯片设置为默认选项?此外,如果只选择了一个芯片,就不能取消选中任何芯片吗?

Gab*_*tti 12

您可以在ChipGroup组件中使用这些属性:

  • app:singleSelection="true". 通过这种方式,ChipGroup可以配置为只允许使用一次检查单个芯片
  • app:checkedChip 定义默认选择

就像是:

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

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ch1"
                android:text="M"/>

            <com.google.android.material.chip.Chip
                style="@style/Widget.MaterialComponents.Chip.Choice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ch2"
                android:text="F"/>

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