Android RadioGroup 单选按钮控件

article/2025/9/22 1:14:13

Android RadioGroup 单选按钮控件

 

RadioGroup 为单项选择按钮组,其中可以包含多个 RadioButton,即单选按钮,它们共同为用户提供一种多选一的选择方式。

在多个 RadioButton 被同一个 RadioGroup 包含的情况下,多个 RadioButton 之间自动形成互斥关系,仅有一个可以被选择。

单选按钮的使用方法和 CheckBox 的使用方法高度相似,其事件监听接口使用的是 RadioGroup.OnCheckedChangeListener(),使用 setOnCheckedChangeListener() 方法将监听器设置到单选按钮上。

按照 CheckBox 的讲解思路,启动一个名为 RadioGroupActivity 的 Activity 来对 RadioGroup 进行讲解。

RadioGroupActivity 的运行效果如图 1 所示。

 

在工程 WidgetDemo 的布局文件 main.xml 中添加一个 Button,并启动 RadioGroupActivity 的相关代码。

在 main.xml 中添加代码如下:

  1. <Button
  2. android:id="@+id/button3"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="RadioGroupDemo"/>

 

启动处理 RadioGroup 的 Activity RadioGroupActivity 的代码如下:

  1. Button radiotn = (Button)this.findViewById(R.id.button3);
  2. radiotn.setOnClickListener(new OnClickListener(){
  3.  
  4. @Override
  5. public void onClick(View v){
  6. Intent intent = new Intent(WidgetDemoActivity.this,RadioGroupActivity.class);
  7. startActivity(intent);
  8. }
  9.  
  10. })

 

同时在 AndroidManifest.xml 文件中声明该 Activity:

<activity android:name=".RadioGroupActivity "></activity>

RadioGroupActivity 使用的是 radiogroup.xml,其代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7.  
  8. <TextView
  9. android:id="@+id/radiohello"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="@string/hello" />
  13.  
  14. <RadioGroup
  15. android:id="@+id/radiogroup1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_x="3px"
  19. android:orientation="vertical">
  20.  
  21. <RadioButton
  22. android:id="@+id/radiobutton1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="@string/music" />
  26.  
  27. <RadioButton
  28. android:id="@+id/radiobutton2"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="@string/gym" />
  32.  
  33. <RadioButton
  34. android:id="@+id/radiobutton3"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:text="@string/dance" />
  38.  
  39. <RadioButton
  40. android:id="@+id/radiobutton4"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="@string/lookBook" />
  44.  
  45. </RadioGroup>
  46. </LinearLayout>

 

该布局文件使用了 LinearLayout 布局,并且在其中放置了一个 TextView 和一个 RadioGroup。RadioGroup 中含有三个 RadioButton。这些组件对应的 strings.xml 文件中定义的变量为:

  1. <resources>
  2. <string name="radiohello">选择你最喜欢的课程:</string>
  3. <string name="music">音乐</string>
  4. <string name="gym">体育</string>
  5. <string name="dance">舞蹈</string>
  6. <string name="lookBook">看书</string>
  7. </resources>

RadioGroupActivity.java 的代码如下:

  1. package introduction.android.widgetdemo;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.RadioButton;
  6. import android.widget.RadioGroup;
  7. import android.widget.TextView;
  8.  
  9.  
  10. public class RadioGroupActivity extends Activity {
  11. private TextView textView;
  12. private RadioGroup radiogroup;
  13. private RadioButton radio1,radio2,radio3,radio4;
  14.  
  15. @Override
  16. protected void onCreate(Bundle saveInstanceState) {
  17. super.onCreate(saveInstanceState);
  18. this.setContentView(R.layout.radiogroup);
  19. textView = (TextView) findViewById(R.id.radiohello);
  20. radiogroup = (RadioGroup)findViewById(R.id.radiogroup1);
  21. radio1 = (RadioButton) findViewById(R.id.radiobutton1);
  22. radio2 = (RadioButton) findViewById(R.id.radiobutton2);
  23. radio3 = (RadioButton) findViewById(R.id.radiobutton3);
  24. radio4 = (RadioButton) findViewById(R.id.radiobutton4);
  25. radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  26. @Override
  27. public void onCheckedChanged(RadioGroup group, int checkedId) {
  28. String text="我最喜欢运动是";
  29. if (checkedId == radio1.getId()) {
  30. text+=radio1.getText().toString();
  31. textView.setText(text);
  32. } else if(checkedId == radio2.getId()){
  33. text+=radio2.getText().toString();
  34. textView.setText(text);
  35. }else if(checkedId == radio3.getId()) {
  36. text += radio3.getText().toString();
  37. textView.setText(text);
  38. }else if(checkedId == radio4.getId()) {
  39. text += radio4.getText().toString();
  40. textView.setText(text);
  41.                 }
  42.             }
  43.         });
  44.     }
  45. }

在 RadioGroupActivity 的 onCreate() 方法中为 RadioGroup 添加监视器 RadioGroup。

OnCheckedChangeListener 在其回调方法 onCheckedChanged() 中对 4 个 RadioButton 分别进行处理。需要说明的是,如果把 RadioGroup 去掉,只使用 RadioButton 的话,则需要为每个 RadioButton 单独设置监听器,其使用方法和 CheckBox 没有任何区别。


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

相关文章

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语言教程视…

数据结构-树

<Python 算法与数据结构视频教程> 学习笔记 1. 什么是树 树结构是一种包括节点(nodes)和边(edges)的拥有层级关系的一种结构 2. 树中的概念 根节点(root): 树的最上层的节点&#xff0c;任何非空的树都有一个节点路径(path): 从起始节点到终止节点经历过的边父亲(par…