Android开发-选择按钮

一、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:滑动开关(推荐)

SwitchToggleButton 的现代化替代品,提供更流畅的滑动切换体验,是设置页面的首选。

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 DesignMaterialCheckBoxMaterialRadioButtonMaterialSwitch,它们提供了更丰富的主题和动画。

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

六、最佳实践与注意事项

  1. 明确选择类型

    • 多选 → CheckBox
    • 单选 → RadioButton + RadioGroup
    • 开关 → Switch(优先于 ToggleButton
  2. 无障碍性(Accessibility)

    • 为每个按钮设置 android:contentDescription
    • 使用 android:labelFor 关联标签。
  3. 状态持久化

    • 用户的选择通常需要保存(如 SharedPreferences),下次打开应用时恢复状态。
  4. 性能优化

    • 大量 CheckBox 列表(如联系人)考虑使用 RecyclerView + ViewHolder 模式。
  5. 用户体验

    • 提供清晰的标签和反馈。
    • 避免过多的选择项导致用户困惑。

七、总结:选择按钮选型指南

控件 选择模式 典型场景 是否推荐
CheckBox 多选 兴趣选择、权限
RadioButton 单选 性别、支付方式
ToggleButton 双态 简单开关 ⚠️(过时)
Switch 双态 设置开关 ✅✅✅(推荐)

八、结语

感谢您的阅读!如果你有任何疑问或想要分享的经验,请在评论区留言交流!

相关推荐
zh_xuan1 天前
Android 待办事项增加事项统计
android
zopple1 天前
Laravel 10.x新特性全解析
android
鬼先生_sir1 天前
MySQL进阶-SQL高级语法全解析
android
Kapaseker1 天前
lazy 与 lateinit 到底有什么区别?
android·kotlin
黄林晴1 天前
慌了!Android 17 取消图标文字,你的 App 可能要找不到了
android
空中海1 天前
3.4 状态同步与生命周期管理
android·网络
斯密码赛我是美女1 天前
【无标题】
android
砖厂小工1 天前
Android 开发的 AI coding 与 AI debugging
android·ai编程
peakmain91 天前
CmComposeUI —— 基于 Kotlin Multiplatform Compose 的 UI 组件库
android
studyForMokey1 天前
【Android面试】Glide专题
android·面试·glide