我如何才能强制MaterialButtonToggleGroup像RadioGroup一样工作,就像始终至少有一个选中的项目一样?如果您单击组中的某个按钮两次,则设置setSingleSelection(true)还增加了不 Select 任何内容的可能性.

Here is my code:

<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"时,如果我点击一个已经选中的按钮,它也会取消选中,在组中没有选中任何按钮.

推荐答案

UPDATE :

app:selectionRequired="true" attribute is available as of version 1.2.0

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.

Kotlin相关问答推荐

在Kotlin中将ClosedRange字符串转换为List?<>

Kotlin:类型不匹配:推断的类型已运行,但应等待

Kotlin 中命名构造函数的惯用方式

Kotlin:我可以将函数分配给 main 的伴随对象中的变量吗?

如何使用函数类型或 lambdas 作为 Kotlin 上下文接收器的类型?

Kotlin 具体类从抽象类和接口扩展而来,接口使用抽象类中实现的方法

如何通过 compose 处理剪切区域?

如何在 IntelliJ 中更改 Kotlin 的this property has a backing field代码编辑器突出显示?

OnClickListener 未在 ConstraintLayout 上触发

Kotlin:内部类如何访问在外部类中声明为参数的变量?

Kotlin 中多个 init 块的用例?

IntentService (kotlin) 的默认构造函数

无法创建类 ViewModel kotlin 的实例

如何在 Kotlin 中为变量设置监听器

Kotlin内联扩展属性

如何在使用 Gradle 的 AppEngine 项目中使用 Kotlin

Jacoco在Gradle 7.0.2和Kotlin 1.5.10上失败

如何解决:将Java类转换为Kotlin后出现error: cannot find symbol class ...?

在kotlin中初始化类变量的正确位置是什么

Kotlin类型安全类型别名