Android入门之路 - RadioGroup、RadioButton、CheckBox(单复选框)使用进阶

article/2025/9/22 0:42:34

本文只为初级的Android新手而写,多掌握一份简单实用的技能,快速get吧,有问题就留言

2022:蓦然回首,已入行多年,人生的第二个迷茫阶段

        • 初级 - 使用方式
          • RadioGroup + RadioButton
          • CheckBox
          • Demo示例
        • CheckBox 自定义选中、取消样式
          • selector 方式
          • style 方式
          • 修改选中状态的颜色
          • CheckBox 点击无效
          • 去除CheckBox默认边距

Let,s gogogo ~

这里写图片描述

初级 - 使用方式

RadioGroup + RadioButton

提醒:使用RadioGroup+RadioButton的时候,如果要实现向我们效果图中一样的效果,需以下操作

  • 首先原有的Button需要设置为null,也就是Xml中的android:button="@null"
  • 同时自己定义一个样式Drawable下创建一个selector,如(图片资源自己找哈)
<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/press_check" android:state_checked="true"></item><item android:drawable="@drawable/nomar_check"></item>
</selector>

xml

    <RadioGroupandroid:padding="5dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rg_gender"android:orientation="horizontal"><RadioButtonandroid:id="@+id/rb_boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:checked="true"android:button="@null"android:drawableLeft="@drawable/gender_bg"android:drawablePadding="5dp"/><RadioButtonandroid:id="@+id/rb_gril"android:layout_width="wrap_content"android:text=""android:button="@null"android:drawablePadding="5dp"android:drawableLeft="@drawable/gender_bg"android:layout_marginLeft="10dp"android:layout_height="wrap_content" /></RadioGroup>

RadioGroup+RadioButton 使用方式

        //多个选项,仅支持单选(常见在主页UI架构)RadioGroup mGender = (RadioGroup) findViewById(R.id.rg_gender);RadioButton mGril = (RadioButton) findViewById(R.id.rb_gril);RadioButton mBoy = (RadioButton) findViewById(R.id.rb_boy);//初始化性别,并没有把下面监听的内容直接set进来,所以需要点击后生效,如果想操作可以在这里直接setTextmBoy.setChecked(true);//单选监听mGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(checkedId==mBoy.getId()){mGenderContent.setText("您老人家竟然是个男的");}else{mGenderContent.setText("您老人家竟然是个女的???");}}});
CheckBox

CheckBox 单独监听

        //复选框 - 单独监听mState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){Toast.makeText(MainActivity.this,"看来你确实是人妖咯",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"你绝对不是人妖!",Toast.LENGTH_SHORT).show();}}});

初级写法

        //多个复选框CheckBox mState1 = (CheckBox) findViewById(R.id.cb_state1);CheckBox mState2 = (CheckBox) findViewById(R.id.cb_state2);CheckBox mState3 = (CheckBox) findViewById(R.id.cb_state3);//多个复选框   因为是初级操作,并没有全部放在list下进行数据集体显示 mState1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState1.getText().toString().trim());Toast.makeText(MainActivity.this,"北京被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState2.getText().toString().trim());Toast.makeText(MainActivity.this,"上海被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState3.getText().toString().trim());Toast.makeText(MainActivity.this,"广州被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});}

常见写法

  		//多个复选框CheckBox mState1 = (CheckBox) findViewById(R.id.cb_state1);CheckBox mState2 = (CheckBox) findViewById(R.id.cb_state2);CheckBox mState3 = (CheckBox) findViewById(R.id.cb_state3);mState1.setOnCheckedChangeListener(this);mState2.setOnCheckedChangeListener(this);mState3.setOnCheckedChangeListener(this);@Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) {if(compoundButton.isChecked())Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();}
Demo示例

MainActivity

package com.example.dow.statelistener;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private CheckBox mState1;private CheckBox mState2;private CheckBox mState3;private CheckBox mState;private RadioButton mBoy;private RadioButton mGril;private RadioGroup mGender;private TextView mGenderContent;private TextView mContent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//内容展示区mGenderContent = (TextView) findViewById(R.id.tv_gender);//单选框mGender = (RadioGroup) findViewById(R.id.rg_gender);mGril = (RadioButton) findViewById(R.id.rb_gril);mBoy = (RadioButton) findViewById(R.id.rb_boy);//复选框mState = (CheckBox) findViewById(R.id.cb_state);//多个复选框mState1 = (CheckBox) findViewById(R.id.cb_state1);mState2 = (CheckBox) findViewById(R.id.cb_state2);mState3 = (CheckBox) findViewById(R.id.cb_state3);mContent = (TextView) findViewById(R.id.tv_content);//初始化性别,并没有把下面监听的内容直接set进来,所以需要点击后生效,如果想操作可以在这里直接setTextmBoy.setChecked(true);//单选监听mGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(checkedId==mBoy.getId()){mGenderContent.setText("您老人家竟然是个男的");}else{mGenderContent.setText("您老人家竟然是个女的???");}}});//复选框 - 单独监听mState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){Toast.makeText(MainActivity.this,"看来你确实是人妖咯",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"你绝对不是人妖!",Toast.LENGTH_SHORT).show();}}});//多个复选框   因为是初级操作,并没有全部放在list下进行数据集体显示 mState1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState1.getText().toString().trim());Toast.makeText(MainActivity.this,"北京被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState2.getText().toString().trim());Toast.makeText(MainActivity.this,"上海被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});mState3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){mContent.setText(mState3.getText().toString().trim());Toast.makeText(MainActivity.this,"广州被选取",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this,"没有选取!",Toast.LENGTH_SHORT).show();}}});}
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.dow.statelistener.MainActivity"><TextViewandroid:layout_width="match_parent"android:id="@+id/tv_gender"android:padding="5dp"android:layout_height="wrap_content"android:text="性别展示区:" /><RadioGroupandroid:padding="5dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/rg_gender"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/rb_boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:checked="true"android:button="@null"android:drawableLeft="@drawable/gender_bg"android:drawablePadding="5dp"/><RadioButtonandroid:id="@+id/rb_gril"android:layout_width="wrap_content"android:text=""android:button="@null"android:drawablePadding="5dp"android:drawableLeft="@drawable/gender_bg"android:layout_marginLeft="10dp"android:layout_height="wrap_content" /></RadioGroup><LinearLayoutandroid:layout_marginTop="30dp"android:layout_width="match_parent"android:orientation="horizontal"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="你是不是人妖?"/><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state"android:layout_height="wrap_content" /></LinearLayout><TextViewandroid:layout_width="match_parent"android:id="@+id/tv_content"android:padding="5dp"android:layout_height="wrap_content"android:text="展示区:" /><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state1"android:text="北京"android:layout_height="wrap_content" /><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state2"android:text="上海"android:layout_height="wrap_content" /><CheckBoxandroid:layout_width="wrap_content"android:id="@+id/cb_state3"android:text="广州"android:layout_height="wrap_content" />
</LinearLayout>

CheckBox 自定义选中、取消样式

这些都是在2022年补入的一些东西,虽然初级,但是还是很常用的... 真实不知不觉这么多年了

selector 方式

drawable 创建 selector 选择器 (我在项目中创建了selector 选择器 - select_login_state

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_enabled="true"android:state_checked="false"android:drawable="@drawable/ic_login_unselected"/><itemandroid:state_enabled="true"android:state_checked="true"android:drawable="@drawable/ic_login_selected"/>
</selector>

引用方式 android:button="@drawable/select 选择器"

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:layout_height="wrap_content" />
style 方式

创建 CheckBoxstyle

CheckBox 无边距(常用)

    <style name="checkboxStyle" parent="Theme.AppCompat.Light"><item name="android:button">@drawable/select_login_state</item></style>

或者 CheckBox 有默认边距

  <style name="checkboxStyle" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="android:button">@drawable/select_login_state</item></style>

xml引用方式 style="@style/checkboxStyle"

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"style="@style/checkboxStyle"android:layout_height="wrap_content" />

修改选中状态的颜色

场景:按理我设置选择器后,选中后为黑色图标,但是老师显示蓝色图标(theme原因)
在这里插入图片描述

创建 CheckBoxstyle

CheckBox 无边距(常用 - UI美观,但有bug)

    <style name="checkboxStyle" parent="Theme.AppCompat.Light"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item><item name="android:button">@drawable/select_login_state</item></style>

或者 CheckBox 有默认边距

    <style name="checkboxStyle" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item></style>

xml引用方式 android:theme="@style/checkboxStyle""android:theme是修改选中颜色的关键属性

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:theme="@style/checkboxStyle"android:button="@drawable/select_login_state"android:layout_height="wrap_content" />

CheckBox 点击无效

首先要看一下发生这个问题的场景是否设置了theme或引用了一些自定义的style?

别人的解决方式:有人说在xml中设置:android:clickable="true" ,但是对我无效!

 <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:clickable="true"android:theme="@style/checkboxStyle"android:layout_height="wrap_content" />

我的解决方式

提示:首先我设置checkBox的方式,都是通过上面的操作实现的

  <CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:theme="@style/checkboxStyle"android:layout_height="wrap_content" />

因为问题主要在theme用到的style,我都说一下

theme style = parent="Theme.AppCompat.Light",导致CheckBox点击无效

    <style name="checkboxStyle" parent="Theme.AppCompat.Light"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item><item name="android:button">@drawable/select_login_state</item></style>

将theme style 改为 parent="@android:style/Widget.CompoundButton.CheckBox",可正常点击,但会出现视图间距、边距(想解决这点,继续往下看)

    <style name="checkboxStyle" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="colorControlNormal">@color/gray</item><item name="colorControlActivated">@color/black</item></style>

去除CheckBox默认边距

关于默认边距问题,主要使用以下俩个属性

 android:minHeight="0dp"android:minWidth="0dp"

例如(不过这样设置过后,有可能点击又无效了

<CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:theme="@style/checkboxStyle"android:minWidth="@dimen/dp_0"android:minHeight="@dimen/dp_0"android:layout_height="wrap_content" />

仅设置android:minWidth="0dp" 属性,可解决CheckBox无法点击的问题

<CheckBoxandroid:id="@+id/iv_check"android:layout_width="wrap_content"android:button="@drawable/select_login_state"android:theme="@style/checkboxStyle"android:minWidth="@dimen/dp_0"android:layout_height="wrap_content" />

http://chatgpt.dhexx.cn/article/rn0qqOuI.shtml

相关文章

Android RadioGroup 单选按钮控件

Android RadioGroup 单选按钮控件 RadioGroup 为单项选择按钮组&#xff0c;其中可以包含多个 RadioButton&#xff0c;即单选按钮&#xff0c;它们共同为用户提供一种多选一的选择方式。在多个 RadioButton 被同一个 RadioGroup 包含的情况下&#xff0c;多个 RadioButton 之间…

RadioGroup控件使用

在只能进行单选的选择上面可以通过&#xff32;adioGroup控件来实现&#xff0c;例如性别选择以及考试的单项选择题。 xml布局如下&#xff1a; <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xm…

RedHat9.0下载地址

RedHat下载&#xff1a;http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/ 转载于:https://www.cnblogs.com/XACOOL/p/5679613.html

下载redhat4.8的方法

一、背景 因为老软件需要安装&#xff0c;所以找个了老系统来安装。 二、上官网 https://www.redhat.com/zh/ 点开redhat最新版本&#xff0c;现在是8.0 下载 要求我登录账户&#xff0c;我就登录jhui163的 然后&#xff0c;看到7.0和更早期的超链接&#xff0c;点进去

vmware安装redhat 8

vmware安装redhat 8 1、下载镜像文件1.1 镜像文件 2、安装系统2.1、选择自定义安装2.2、兼容性选择2.3、选择镜像文件导入2.4、设置用户名密码2.5、选择虚拟机在磁盘上的位置2.6、选择处理器数量2.7、选择内存大小2.8、选择桥接或NAT2.9、选择SCSI控制器类型2.10、选择虚拟机磁…

RedHat 7.5 7.6下载磁力链分享

某度最近更新了一波&#xff0c;导致诸多屏蔽弹客户端应用直接显示直链下载的浏览器插件也失效&#xff0c;就连最强的下载神器也400、403报错 csdn的下载站里是有不少资源&#xff0c;但是苦于都要积分&#xff0c;出于服务大众、便利人民的心。 我终于找到了对应的下载磁力…

RedHat使用yum下载安装软件包

RedHat使用yum下载安装软件包 Red Hat Enterprise Linux Server&#xff08;RHEL&#xff09;的yum服务是收费的&#xff0c;如果没有付费&#xff0c;则无法使用yum安装软件包。通过删除RedHat自带的yum&#xff0c;安装CentOS版本的yum&#xff0c;并使用CentOS的yum源和epel…

RedHat红帽RHEL7.2镜像下载以及安装教程(内含下载链接)

RedHat红帽RHEL7.2镜像下载以及安装教程 镜像下载链接&#xff1a; https://pan.baidu.com/s/1czcz-ClYavcugE9PJDIIQg?pwdq2t1 提取码:q2t1 安装教程 1、打开VM&#xff0c;新建虚拟机 2、选择自定义&#xff0c;下一步 3、默认&#xff0c;下一步 4、选择稍后安装操作系统…

redhat官方文档下载方法

打开地址&#xff1a;https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/ 如图下载pdf格式文件&#xff1a;

安装Redhat

1.新建虚拟机&#xff0c;选典型 2.下一步&#xff0c;选择稍后安装操作系统 3.下一步&#xff0c;选择Linux&#xff0c;版本选择Red Hat Enterprise 8&#xff08;版本是什么就选择什么&#xff09; 4.下一步&#xff0c;设置虚拟机名称以及位置 5.下一步&#xff0c;设…

红帽linux6.8镜像下载,redhat8镜像下载

https://developers.redhat.com/rhel8 点击下图的“Download RHEL” 点击完 “Download RHEL”后会跳转到登录界面&#xff0c;注册个账号登录即可&#xff0c;该文档将跳过注册步骤 截止该文档下载时&#xff0c;redhat8的版本为8.3 beat版本&#xff0c;点击“View Older Dow…

redhat下载gcc

redhat似乎受众很少&#xff0c;安装gcc很少有参照。 倒腾很久终于安装成功了&#xff0c;浅浅记录一下。 1、首先将创建一个gcc文件夹&#xff0c;将gcc挂载到/dev/sr0下 2、然后进入繁琐的安装过程。 &#xff08;大抵是需要这些包了&#xff0c;但是不同rhel可能版本号不同…

如何下载redhat enterprise版本

1.登录网站: developers.redhat.com 2.点开下面红框内的图标 3.再点下面红框内的图标 4. 继续点红框内的图标 5.点开以后是下面的页面&#xff0c;往下拖动就可以看见redhat enterprise 的很多版本了 备注&#xff1a;以上网站有时候下载一段时间就断开不能下载了。也可以从下…

Red Hat Enterprise Linux RHEL 8.6 下载安装

前言 由于 CentOS 长期以来没有为 Red Hat 增加价值&#xff0c;Red Hat 停止了 CentOS Linux 的维护&#xff0c;为了留住小规模 CentOS 用户&#xff0c;Red Hat 允许免费下载 RHEL&#xff0c;在 2021 年 1 月&#xff0c;红帽宣布个人可以获得免费的个人订阅&#xff0c;以…

red hat 系统下载

red hat linux 系统下载 在官方下载需要有账号&#xff0c;如果没有账号可到百度云下载&#xff08;百度云的速度你知道&#xff09; https://pan.baidu.com/s/1gRuUdTFqnKP9a4yy6y2rbg 官方下载地址&#xff1a;https://developers.redhat.com/products/rhel/download/ 1.…

Java算法与数据结构、设计模式、高并发视频教程免费下载

Java算法与数据结构、设计模式、高并发视频教程免费下载&#xff01; 链接&#xff1a;http://pan.baidu.com/s/1gfyobmF 密码&#xff1a;bef5 链接我就不放出来了&#xff0c;太容易失效。需要这套视频教的网页&#xff0c;可以扫描下方的微信二维码&#xff0c;关注“业余…

Python数据结构与算法视频教程-王宁宁-专题视频课程

Python数据结构与算法视频教程—367人已学习 课程介绍 Python数据结构与算法视频培训教程&#xff1a;本课程内容包含了程序员常用的数据结构知识&#xff0c;涉及快速排序、树与二叉树、堆、堆排序、图的概念与遍历、Python常用的内置算法与数据结构等开发知识。数据结构和…

数据结构与算法(python版)

最近学习数据结构&#xff0c;对于从未接触过数据结构的我来说&#xff0c;老师不仅讲解理论&#xff0c;还有代码的逐层分析&#xff0c;非常不错&#xff0c;受益匪浅&#xff01;&#xff01;&#xff01;&#xff08;以下是学习记录&#xff09; 黑马程序员孔德海老师教程…

数据结构与算法经典图书推荐

这些年来&#xff0c;阅读了大量关于数据结构与算法方面的图书&#xff0c;包括教材和店面用书&#xff0c;对于数据结构与算法方面的学习&#xff0c;积累了一些心得&#xff0c;其实很想告诉刚刚和即将接触计算机的朋友们关于如何学习数据结构与算法、如何规划今后的学习&…

c语言的数据结构,c语言中数据结构是什么?常见数据结构有哪些?

c语言中,数据结构是指相互之间存在一种或多种特定关系的数据元素的集合,它是计算机存储、组织数据的方式;常见数据结构有:线性数据结构(数组、链表、栈、队列和线性表)、树形结构(二叉树、完全二叉树、二叉查找树、堆)、图形结构(有向图和无向图)。 教程推荐:《c语言教程视…