android - Material 按钮切换组单选

标签 android kotlin android-button

如何强制 MaterialButtonToggleGroup 像 RadioGroup 一样总是至少有一个选定的项目?设置setSingleSelection(true)如果您在组中的按钮上单击两次,还增加了不选择任何内容的可能性。

这是我的代码:

<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>

如您所见,即使在使用 应用程序:singleSelection="true"如果我点击一个已经选中的按钮,它会取消选中它,在组中没有选中任何按钮。

最佳答案

更新 :app:selectionRequired="true"属性从 1.2.0 版开始可用
覆盖 toggle() MaterialButton 的方法类并使用它而不是 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()
        }
    }
}
这将确保已选中的按钮不会在单个选择中取消选中。

关于android - Material 按钮切换组单选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57090690/

相关文章:

java - Android 将加速度计读数保存到浮点变量时出现错误?

java - Android twitter4j 自定义 API key

Android studio自定义按钮样式填充问题

android - 从 Button onClick 获取 TextView 的值并通过 TextWatcher 将值传递给 TextView

Android:通过“主页”按钮(或“音量”或“键盘锁”按钮)单击启动应用程序

android - 真的很困惑 setPreviewCallback 在 Android 中,需要建议

android - Android 上的透明 LinearLayout

java - 每次都需要重建项目以反射(reflect) Android Studio Chipmunk 中的变化

Kotlin 反射不可用

android - 当我们在 kotlin 中开始新的 Activity 时,为什么我们将 .java 放在 intent 中而不是 .kt