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 双态 设置开关 ✅✅✅(推荐)

八、结语

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

相关推荐
火柴就是我2 小时前
android 以maven的方式 引入本地的aar
android
过-眼-云-烟2 小时前
新版Android Studio能打包但无法run ‘app‘,编译通过后手机中没有安装,顶部一直转圈
android·ide·android studio
hedalei3 小时前
android14 硬键盘ESC改BACK按键返回无效问题
android·android14·esc·back按键
hcgeng3 小时前
android 如何判定底部导航栏显示时 不是键盘显示
android·底部导航·导航高度
和煦的春风3 小时前
性能案例分析 | Waiting for GPU completion
android·linux
用户2018792831673 小时前
ConcurrentHashMap:用 “社区超市” 故事讲透并发的设计哲学
android
4Forsee4 小时前
【Android】View 交互的事件处理机制
android·交互
龙腾-虎跃4 小时前
Android Sip电话(PJSP)
android
zhangphil4 小时前
Android点击桌面图库应用启动流程trace分析
android