Mer*_*aju 4 android android-button kotlin
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.
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>
Run Code Online (Sandbox Code Playgroud)
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.
Nic*_*rth 17
从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>
Run Code Online (Sandbox Code Playgroud)
现在您可以使用该app:selectionRequired属性来实现它。
就像是:
<com.google.android.material.button.MaterialButtonToggleGroup
app:singleSelection="true"
app:selectionRequired="true"
app:checkedButton="@id/..."
..>
Run Code Online (Sandbox Code Playgroud)
您也可以以编程方式使用该方法setSelectionRequired:
buttonGroup.setSelectionRequired(true);
Run Code Online (Sandbox Code Playgroud)
请注意,此属性至少需要1.2.0-alpha03版本
重写类的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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将确保未选中单个选择中已选中的按钮。
| 归档时间: |
|
| 查看次数: |
765 次 |
| 最近记录: |