本文介绍了ChipGroup单选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何强制ChipGroup
像RadioGroup
一样,始终具有至少一个选定的项目?如果在Chip
上单击两次,则设置setSingleSelection(true)
还会增加不选择任何内容的可能性.
How can I force a ChipGroup
to act like a RadioGroup
as in having at least one selected item always? Setting setSingleSelection(true)
also adds the possibility to have nothing selected if you click twice on a Chip
.
推荐答案
要防止取消选择所有筹码,可以使用方法 setSelectionRequired
:
To prevent all chips from being deselected you can use the method setSelectionRequired
:
chipGroup.setSelectionRequired(true)
您还可以使用 app:selectionRequired
属性在布局中对其进行定义:
You can also define it in the layout using the app:selectionRequired
attribute:
<com.google.android.material.chip.ChipGroup
app:singleSelection="true"
app:selectionRequired="true"
app:checkedChip="@id/..."
..>
注意:这要求最低版本为 1.2.0-alpha02
Note: This requires a minimum of version 1.2.0-alpha02
这篇关于ChipGroup单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!