一、CheckBox:多选框(可多选)
CheckBox
允许用户从一组选项中选择一个或多个项目。
1. 基本使用
XML
<!-- 布局文件 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<CheckBox
android:id="@+id/cb_java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:checked="true" />
<CheckBox
android:id="@+id/cb_kotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin" />
<CheckBox
android:id="@+id/cb_python"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Python" />
</LinearLayout>
2. 监听状态变化
java
CheckBox cbJava = findViewById(R.id.cb_java);
CheckBox cbKotlin = findViewById(R.id.cb_kotlin);
CheckBox cbPython = findViewById(R.id.cb_python);
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String skill = buttonView.getText().toString();
if (isChecked) {
Log.d("CheckBox", "选择了: " + skill);
} else {
Log.d("CheckBox", "取消了: " + skill);
}
// 更新你的数据模型
}
};
cbJava.setOnCheckedChangeListener(listener);
cbKotlin.setOnCheckedChangeListener(listener);
cbPython.setOnCheckedChangeListener(listener);
✅ 适用场景:兴趣选择、权限申请、多选设置。
二、RadioButton:单选按钮(二选一或多选一)
RadioButton
用于从一组互斥 的选项中选择一个 。通常与 RadioGroup
配合使用。
1. 基本使用(配合 RadioGroup)
XML
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true" />
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
2. 监听选择变化
java
RadioGroup rgGender = findViewById(R.id.rg_gender);
rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_male:
Log.d("RadioGroup", "选择了: 男");
break;
case R.id.rb_female:
Log.d("RadioGroup", "选择了: 女");
break;
}
// 更新性别选择
}
});
3. 获取当前选中项
java
int selectedId = rgGender.getCheckedRadioButtonId();
if (selectedId != -1) {
RadioButton selectedRadioButton = findViewById(selectedId);
String gender = selectedRadioButton.getText().toString();
}
✅ 适用场景:性别选择、支付方式、单选设置。
三、ToggleButton:开关按钮(两种状态)
ToggleButton
是一个简单的"开/关"切换按钮,显示"ON"和"OFF"文本。
1. 基本使用
XML
<ToggleButton
android:id="@+id/tb_flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="手电筒开"
android:textOff="手电筒关"
android:checked="false" />
2. 监听状态变化
java
ToggleButton tbFlashlight = findViewById(R.id.tb_flashlight);
tbFlashlight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.d("ToggleButton", "手电筒已开启");
// 开启手电筒逻辑
} else {
Log.d("ToggleButton", "手电筒已关闭");
// 关闭手电筒逻辑
}
}
});
⚠️ 注意 :
ToggleButton
的文本是固定的,不够现代化。
四、Switch:滑动开关(推荐)
Switch
是 ToggleButton
的现代化替代品,提供更流畅的滑动切换体验,是设置页面的首选。
1. 基本使用
XML
<Switch
android:id="@+id/sw_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启通知"
android:checked="true" />
2. 自定义文字
XML
<Switch
android:id="@+id/sw_dark_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="深色模式"
android:textOn="开"
android:textOff="关"
android:checked="false" />
3. 监听状态变化
与 ToggleButton
相同,使用 setOnCheckedChangeListener
。
✅ 优势:
- 视觉效果更现代。
- 用户体验更佳。
- Material Design 推荐控件。
五、自定义选择按钮样式
1. 自定义 CheckBox/RadioButton 图标
创建 res/drawable/checkbox_selector.xml
:
XML
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_checkbox_checked"
android:state_checked="true" />
<item android:drawable="@drawable/ic_checkbox_unchecked"
android:state_checked="false" />
<!-- 默认状态 -->
<item android:drawable="@drawable/ic_checkbox_unchecked" />
</selector>
在 CheckBox
中应用:
XML
<CheckBox
android:button="@drawable/checkbox_selector"
... />
2. 自定义 Switch 样式
XML
<Switch
android:id="@+id/sw_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义开关"
android:thumb="@drawable/thumb_selector" <!-- 滑块 -->
android:track="@drawable/track_selector" <!-- 轨道 -->
android:checked="true" />
3. 使用 Material Design 组件
推荐使用 Material Design
的 MaterialCheckBox
、MaterialRadioButton
、MaterialSwitch
,它们提供了更丰富的主题和动画。
Groovy
implementation 'com.google.android.material:material:1.11.0'
XML
<com.google.android.material.checkbox.MaterialCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Material 复选框"
app:buttonTint="@color/primary_color" />
六、最佳实践与注意事项
-
明确选择类型:
- 多选 →
CheckBox
- 单选 →
RadioButton
+RadioGroup
- 开关 →
Switch
(优先于ToggleButton
)
- 多选 →
-
无障碍性(Accessibility):
- 为每个按钮设置
android:contentDescription
。 - 使用
android:labelFor
关联标签。
- 为每个按钮设置
-
状态持久化:
- 用户的选择通常需要保存(如
SharedPreferences
),下次打开应用时恢复状态。
- 用户的选择通常需要保存(如
-
性能优化:
- 大量
CheckBox
列表(如联系人)考虑使用RecyclerView
+ ViewHolder 模式。
- 大量
-
用户体验:
- 提供清晰的标签和反馈。
- 避免过多的选择项导致用户困惑。
七、总结:选择按钮选型指南
控件 | 选择模式 | 典型场景 | 是否推荐 |
---|---|---|---|
CheckBox |
多选 | 兴趣选择、权限 | ✅ |
RadioButton |
单选 | 性别、支付方式 | ✅ |
ToggleButton |
双态 | 简单开关 | ⚠️(过时) |
Switch |
双态 | 设置开关 | ✅✅✅(推荐) |
八、结语
感谢您的阅读!如果你有任何疑问或想要分享的经验,请在评论区留言交流!