Android 材料按钮和开关组上所需的单个选择
使用Android 材料按钮和开关组上所需的单个选择,android,android-button,material-components-android,material-components,android-togglebutton,Android,Android Button,Material Components Android,Material Components,Android Togglebutton,使用app:singleSelection=“true”时,是否有选项使MaterialButtonToggleGroup具有所需的选定按钮 当单击其他按钮时效果良好(所有其他按钮都被取消选中),但当您单击相同(已选中)按钮时,它会取消选中它本身,我希望保持选中状态 我的例子是: <com.google.android.material.button.MaterialButtonToggleGroup android:layout_width="wrap" and
app:singleSelection=“true”
时,是否有选项使MaterialButtonToggleGroup
具有所需的选定按钮
当单击其他按钮时效果良好(所有其他按钮都被取消选中),但当您单击相同(已选中)按钮时,它会取消选中它本身,我希望保持选中状态
我的例子是:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap"
android:layout_height="wrap_content"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/filterA"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/filterB"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/filterC"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
我也遇到了这个问题,将等待谷歌的永久修复。同时,我做了以下操作以确保至少有一个按钮被选中
final MaterialButtonToggleGroup tGroup = view.findViewById(R.id.toggleGroup);
final MaterialButton breast = tGroup.findViewById(R.id.breast);
final MaterialButton bottle = tGroup.findViewById(R.id.bottle);
final MaterialButton solids = tGroup.findViewById(R.id.solids);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
tGroup.check(v.getId());
}
};
breast.setOnClickListener(onClickListener);
bottle.setOnClickListener(onClickListener);
solids.setOnClickListener(onClickListener);
希望这能有所帮助。我也遇到了这个问题,我发现这个问题与
应用程序:singleSelection=“true”
我是通过以下方式得到这一结果的:
toggle_group.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (group.checkedButtonId == -1) group.check(checkedId)
}
如果启用了singleSelection,则仅当用户单击已选中的按钮时,条件才会计算为true,从而使其不选中任何按钮。发生这种情况时,我们只需检查未选中的按钮。您可以使用
应用程序:selectionRequired
属性在布局中定义它:
<com.google.android.material.button.MaterialButtonToggleGroup
app:selectionRequired="true"
app:checkedButton="@id/..."
app:singleSelection="true">
注意:这需要最低版本1.2.0-alpha03我已经填写了一个功能请求,因为我找不到合适的解决方案。应用程序:singleSelection=“true”对我不起作用。所有按钮仍然可以同时选择选中时如何映射子按钮?谢谢。这是最简单的解决方案,不依赖于alpha版本。是的,你是对的,谷歌团队添加了此功能->
<com.google.android.material.button.MaterialButtonToggleGroup
app:selectionRequired="true"
app:checkedButton="@id/..."
app:singleSelection="true">
buttonGroup.setSelectionRequired(true);