ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 1. MaterialButtonToggleGroup 类似于在前端开发中的单选组和复选组的意思。可以将多个`MaterialButton`组和为一个组,控制其操作。 # 2. 属性 | 属性 | 描述 | 参数| | --- | --- | --- | | `app:checkedButton` | 默认选中 | 按钮`ID`| | `app:singleSelection` | 单选| `true`/`false` | | `app:selectionRequired` | 设置为`true`表示至少选中一个 | `true`/`false` | # 3. 案例 ~~~ <com.google.android.material.button.MaterialButtonToggleGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:singleSelection="true" app:checkedButton="@id/btn_1" android:id="@+id/group" > <com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_1" android:text="Button1" android:textSize="18sp" > </com.google.android.material.button.MaterialButton> <com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" android:textSize="18sp" > </com.google.android.material.button.MaterialButton> <com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" android:textSize="18sp" > </com.google.android.material.button.MaterialButton> </com.google.android.material.button.MaterialButtonToggleGroup> ~~~ 当然可以添加点击响应监听事件: ~~~ val group = findViewById<MaterialButtonToggleGroup>(R.id.group) group.addOnButtonCheckedListener(object : MaterialButtonToggleGroup.OnButtonCheckedListener{ override fun onButtonChecked( group: MaterialButtonToggleGroup?, checkedId: Int, isChecked: Boolean ) { Log.e("TAG", "Message: $checkedId") } }) ~~~