RadioGroup

article/2025/9/22 6:08:41

实现RadioButton由两部分组成,也就是RadioButtonRadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButtonRadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听

RadioGroup相关属性:RadioGroup.getCheckedRadioButtonId ();--获取选中按钮的idRadioGroup.clearCheck ();//---清除选中状态RadioGroup.check (int id);//---通过参入选项id来设置该选项为选中状态如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener); //--一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数addView (View child, int index, ViewGroup.LayoutParams params);//---使用指定的布局参数添加一个子视图//参数 child 所要添加的子视图    index 将要添加子视图的位置  params 所要添加的子视图的布局参数RadioButton.getText();//获取单选框的值//此外,RadioButton的checked属性设置为true,代码里调用RadioButton的check(id)方法,不会触发onCheckedChanged事件

RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

2、每个RadioGroup中的RadioButton同时只能有一个被选中

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

4、大部分场合下,一个RadioGroup中至少有2个RadioButton

5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

看案例:

1.定义布局文件:

 

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginRight="5dp" ><TextViewandroid:id="@+id/radiogroup_info_id"android:layout_width="228px"android:layout_height="wrap_content"android:text="我选择的是...?"android:textSize="30sp"    />   <RadioGroup android:id="@+id/radioGroup_sex_id"android:layout_width="match_parent"android:layout_height="match_parent" ><RadioButton android:id="@+id/boy_id"android:layout_width="match_parent"android:layout_height="match_parent"   android:text="Boy"/><RadioButton android:id="@+id/girl_id"android:layout_width="match_parent"android:layout_height="match_parent"   android:text="Girl"/></RadioGroup><Button android:id="@+id/radio_clear"android:layout_width="match_parent"android:layout_height="match_parent"   android:text="清除选中按钮"/><Button android:id="@+id/radio_add_child"android:layout_width="match_parent"android:layout_height="match_parent"   android:text="添加单选项"/></LinearLayout></ScrollView>

 2.java代码文件

 

package com.dream.app.start.first.radiobutton;import com.dream.app.start.R;
import com.dream.app.start.R.id;
import com.dream.app.start.R.layout;
import com.dream.app.start.three.utils.PublicClass;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;public class RadioButtonDemo extends PublicClass {private  TextView   textView=null;private  RadioGroup  radioGroup=null;private  RadioButton  radioButton_boy,radioButton_girl;private Button   radio_clear,child;/* (non-Javadoc)* @see  android.app.Activity#onCreate(android.os.Bundle)*/@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.layout_frist_radiobuton);textView = (TextView)findViewById(R.id.radiogroup_info_id);  //radioGroupradioGroup=(RadioGroup)findViewById(R.id.radioGroup_sex_id);radioButton_boy=(RadioButton)findViewById(R.id.boy_id);radioButton_girl=(RadioButton)findViewById(R.id.girl_id);child=(Button)findViewById(R.id.radio_add_child);//---radioGroup.setOnCheckedChangeListener(listen);radio_clear=(Button)findViewById(R.id.radio_clear);radio_clear.setOnClickListener(onClick);child.setOnClickListener(onClick);}private OnCheckedChangeListener  listen=new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {int id=	group.getCheckedRadioButtonId();switch (group.getCheckedRadioButtonId()) {case R.id.girl_id:textView.setText("我选择的是:"+radioButton_girl.getText());break;case R.id.boy_id:textView.setText("我选择的是:"+radioButton_boy.getText());break;default:textView.setText("我选择的是:新增");break;}}};private OnClickListener  onClick=new OnClickListener() {@Overridepublic void onClick(View v) {radio_clear=(Button)v;switch (radio_clear.getId()) {case R.id.radio_clear:radioGroup.check(-1);//清除选项
//				radioGroup.clearCheck(); //清除选项textView.setText("我选择的是...?");break;case R.id.radio_add_child://新增子RadioButton  newRadio =new RadioButton(getApplicationContext());newRadio.setText("新增");radioGroup.addView(newRadio, radioGroup.getChildCount()); 				break;//default:break;}}};}

运行效果:

运行效果

选中一个按钮时的截图

 

3.

点击添加单选项的截图

 

 4:可以通过设置如下属性可以使单选按钮在显示文本的右边

                android:button="@null"                    

                android:drawableRight="@android:drawable/btn_radio"

效果:

 

RadioButton和CheckBox的区别:

1、单个RadioButton在选中后,通过点击无法变为未选中

    单个CheckBox在选中后,通过点击可以变为未选中

2、一组RadioButton,只能同时选中一个

     一组CheckBox,能同时选中多个

3、RadioButton在大部分UI框架中默认都以圆形表示

     CheckBox在大部分UI框架中默认都以矩形表示

==================================================

☆定制RadioButton样式

RadioButton长成什么样子是由其Background、Button等属性决定的,Android系统 
使用style定义了默认的属性,在android源码 

android/frameworks/base/core/res/res/values/styles.xml中可以看到默认的定义:

<style name="Widget.CompoundButton.RadioButton">  <item name="android:background">@android:drawable/btn_radio_label_background</item>  <item name="android:button">@android:drawable/btn_radio</item>  
</style>

即其背景图是btn_radio_label_background,其button的样子是btn_radio 

btn_radio_label_background是什么? 
其路径是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_label_background.9.png 
可以看到是一个NinePatch图片,用来做背景,可以拉伸填充。 

btn_radio是什么? 

其路径是android/frameworks/base/core/res/res/drawable/btn_radio.xml 

是个xml定义的drawable,打开看其内容:

<selector xmlns:android="http://schemas.android.com/apk/res/android">      <item android:state_checked="true" android:state_window_focused="false"  android:drawable="@drawable/btn_radio_on" />  <item android:state_checked="false" android:state_window_focused="false"  android:drawable="@drawable/btn_radio_off" />  <item android:state_checked="true" android:state_pressed="true"  android:drawable="@drawable/btn_radio_on_pressed" />  <item android:state_checked="false" android:state_pressed="true"  android:drawable="@drawable/btn_radio_off_pressed" />  <item android:state_checked="true" android:state_focused="true"  android:drawable="@drawable/btn_radio_on_selected" />  <item android:state_checked="false" android:state_focused="true"  android:drawable="@drawable/btn_radio_off_selected" />  <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />  <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />  
</selector>

<item android:state_checked="true" android:state_pressed="true"  
          android:drawable="@drawable/btn_radio_on_pressed" />  

意思即为当radiobutton被选中时,并且被按下时,其Button应该长成btn_radio_on_pressed这个样子。


文件是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_on_pressed.png 

drawable的item中可以有以下属性: 

android:drawable="@[package:]drawable/drawable_resource" 
android:state_pressed=["true" | "false"] 
android:state_focused=["true" | "false"] 
android:state_selected=["true" | "false"] 
android:state_active=["true" | "false"] 
android:state_checkable=["true" | "false"] 
android:state_checked=["true" | "false"] 
android:state_enabled=["true" | "false"] 
android:state_window_focused=["true" | "false"]

当按钮的状态和某个item匹配后,就会使用此item定义的drawable作为按钮图片。  

从上面分析我们如果要修改RadioButton的外观,

自定义有三种方式:

1.方式一:

<?xml version="1.0" encoding="utf-8"?>     
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
<!-- 未选中->   <item     android:state_checked="false"     android:drawable="@drawable/tabswitcher_long" />  
<!--选中->     <item     android:state_checked="true"     android:drawable="@drawable/tabswitcher_short" />     
</selector>

在布局文件中使用

<RadioGroup  ...  
>  
<RadioButton  ...  
android:button="@null"  
android:background="@drawable/radio"  
/>  
</RadioGroup>

android:button="@null"   去除RadioButton前面的圆点

2.方式二:在JAVA代码中定义

@Override   
public boolean onTouchEvent(MotionEvent event) {  if(event.getActionMasked() == MotionEvent.ACTION_DOWN){  this.setBackgroundResource(com.wxg.tab.R.drawable.main_bg);  }else if(event.getActionMasked()== MotionEvent.ACTION_DOWN) {  this.setBackgroundResource(com.wxg.tab.R.drawable.hui);  }  return super.onTouchEvent(event);  
}

去除RadioButton前面的圆点adioButton.setButtonDrawable(android.R.color.transparent);

3. 方式三

使用XML文件定义,在JAVA代码中使用 radioButton.setBackgroundResource(R.drawable.radio);调用

==============================================================

                                                设置RadioButton在文字的右边

<RadioButton android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="50dip" android:button="@null" android:drawableRight="@android:drawable/btn_radio" //在右边 android:paddingLeft="30dip" android:text="Android高手" android:textSize="20dip" /> 

================================================================

自定义 radiobutton 文字颜色随选中状态而改变

主要是写一个 color selector

在res/建一个文件夹取名color

res/color/color_radiobutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true"  android:color="@color/color_text_selected"/><!-- not selected --><item android:color="@color/color_text_normal"/>
</selector>

布局文件定义控件:
<RadioButtonandroid:id="@+id/radiobutton_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/selector_radio"android:button="@null"android:checked="true"android:gravity="center"android:text="目录"<!--自定义文本颜色  -->android:textColor="@color/color_radiobutton" android:textSize="@dimen/font_size"android:textStyle="bold" />

============================================================
RadioButton上显示图片和文字

使用XML文件很简单就可以实现,但是有时必须要使用java code 的方式动态实现,这就有些复杂了,这需要继承RadioButton并覆盖其中的onDraw方法。

在代码中的image是Bitmap对象。

@Override  protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  if(image!=null){  Paint pt = new Paint();  pt.setARGB(255,66,66,66);  //消除锯齿  pt.setAntiAlias(true);  //居中显示图片  int imageX=(int)(this.getWidth()-image.getWidth())/2;  canvas.drawBitmap(image,imageX,5,pt);  pt.setARGB(255,255,255,255);  //居中显示字符串  int strX=(int)(this.getWidth()-name.getBytes().length*5.5)/2;  canvas.drawText(name,strX,(image.getHeight()+15),pt);  } }

 




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

相关文章

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

本文只为初级的Android新手而写&#xff0c;多掌握一份简单实用的技能&#xff0c;快速get吧&#xff0c;有问题就留言 2022&#xff1a;蓦然回首&#xff0c;已入行多年&#xff0c;人生的第二个迷茫阶段 初级 - 使用方式RadioGroup RadioButtonCheckBoxDemo示例 CheckBox 自…

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;其实很想告诉刚刚和即将接触计算机的朋友们关于如何学习数据结构与算法、如何规划今后的学习&…