物料按钮切换组单选 [英] Material Button Toggle Group single selection

查看:97
本文介绍了物料按钮切换组单选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制 MaterialButtonToggleGroup 像无线电组一样,始终具有至少一个选定的项目?设置setSingleSelection(true)还可以增加在组中的按钮上单击两次的可能性.

How can I force a MaterialButtonToggleGroup 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 Button in the group.

这是我的代码:

<com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@id/BottomSheetDialog_fromFragmentBottomSheetSort_Sort_ToggleButtonGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:singleSelection="true"
            app:checkedButton="@+id/BottomSheetDialog_fromFragmentBottomSheetSort_Sort_ToggleButtonGroup_Ascending">

        <com.google.android.material.button.MaterialButton
                android:id="@id/BottomSheetDialog_fromFragmentBottomSheetSort_Sort_ToggleButtonGroup_Ascending"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Fragment_BottomSheetDialog_Sort_ToggleButton_Ascending"
                app:backgroundTint="@color/custom_button_background_states"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"/>

        <com.google.android.material.button.MaterialButton
                android:id="@id/BottomSheetDialog_fromFragmentBottomSheetSort_Sort_ToggleButtonGroup_Descending"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Fragment_BottomSheetDialog_Sort_ToggleButton_Descending"
                app:backgroundTint="@color/custom_button_background_states"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"/>
    </com.google.android.material.button.MaterialButtonToggleGroup>

如您所见,即使在使用 app:singleSelection ="true" 时,如果我单击一个已经选中的按钮,它也会取消选中它,而不会在组中选中任何按钮.

As you can see, even while using app:singleSelection="true" if i click on an already checked button, it unchecks it leaving no button checked in the group.

推荐答案

重写MaterialButton类的toggle()方法并使用它代替MaterialButton

Override the toggle() method of the MaterialButton class and use it instead of MaterialButton

import android.content.Context
import android.util.AttributeSet
import com.google.android.material.button.MaterialButton

class CustomMaterialToggleButton : MaterialButton {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun toggle() {
        if (!isChecked) {
            super.toggle()
        }
    }
}

这将确保未选中单个选择中已选中的按钮.

This will make sure that already checked button is not unchecked on single selection.

这篇关于物料按钮切换组单选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆