Material 按钮切换组单选
Material Button Toggle Group single selection
如何强制 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>
如您所见,即使在使用 app:singleSelection="true" 时,如果我单击一个已经选中的按钮,它会取消选中它,而不会在组.
如果你真的想这样做,你可以在 Kotlin 中这样做。
toggle_group.forEach { button ->
button.setOnClickListener { (button as MaterialButton).isChecked = true }
}
这将防止第二次点击取消选中。
更新:
app:selectionRequired="true"
属性从版本 1.2.0 开始可用
覆盖 MaterialButton
class 的 toggle()
方法并使用它代替 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()
}
}
}
这将确保在单次选择时不会取消选中已选中的按钮。
从 1.2.0-alpha03 开始,您可以简单地使用 selectionRequired
选项:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_button_group"
app:singleSelection="true"
app:selectionRequired="true">
</com.google.android.material.button.MaterialButtonToggleGroup>
现在您可以使用 app:selectionRequired
属性来实现它。
类似于:
<com.google.android.material.button.MaterialButtonToggleGroup
app:singleSelection="true"
app:selectionRequired="true"
app:checkedButton="@id/..."
..>
您也可以通过编程方式使用方法 setSelectionRequired
:
buttonGroup.setSelectionRequired(true);
请注意此属性需要最低版本1.2.0-alpha03
如何强制 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>
如您所见,即使在使用 app:singleSelection="true" 时,如果我单击一个已经选中的按钮,它会取消选中它,而不会在组.
如果你真的想这样做,你可以在 Kotlin 中这样做。
toggle_group.forEach { button ->
button.setOnClickListener { (button as MaterialButton).isChecked = true }
}
这将防止第二次点击取消选中。
更新:
app:selectionRequired="true"
属性从版本 1.2.0 开始可用
覆盖 MaterialButton
class 的 toggle()
方法并使用它代替 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()
}
}
}
这将确保在单次选择时不会取消选中已选中的按钮。
从 1.2.0-alpha03 开始,您可以简单地使用 selectionRequired
选项:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_button_group"
app:singleSelection="true"
app:selectionRequired="true">
</com.google.android.material.button.MaterialButtonToggleGroup>
现在您可以使用 app:selectionRequired
属性来实现它。
类似于:
<com.google.android.material.button.MaterialButtonToggleGroup
app:singleSelection="true"
app:selectionRequired="true"
app:checkedButton="@id/..."
..>
您也可以通过编程方式使用方法 setSelectionRequired
:
buttonGroup.setSelectionRequired(true);
请注意此属性需要最低版本1.2.0-alpha03