Android控件系列之RadioButton&RadioGroup

摘要:
˃27131415161724所选项目更改的事件监视:当RadioGroup中的所选项目发生更改时,您可能需要执行某些操作,例如,在上面的示例中,性别选择为”女性““下面的文章也会相应地改变,或者在选择了不同的性别后,将更新与该性别匹配的头像列表,女孩将不喜欢使用胡须作为自己的头像。如果你不熟悉听众,可以阅读Android控制系列的Button和Android听众。”。

学习目的:

1、掌握在Android中如何建立RadioGroup和RadioButton

2、掌握RadioGroup的常用属性

3、理解RadioButton和CheckBox的区别

4、掌握RadioGroup选中状态变换的事件(监听器)

Android控件系列之RadioButton&RadioGroup第1张

RadioButton和CheckBox的区别:

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

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

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

     一组CheckBox,能同时选中多个

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

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

RadioButton和RadioGroup的关系:

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

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

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

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

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

XML布局:

1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7  <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="请选择您的性别:"
11 android:textSize="9pt"
12 />
13  <RadioGroup android:id="@+id/radioGroup" android:contentDescription="性别" android:layout_width="wrap_content" android:layout_height="wrap_content">
14 <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioMale" android:text="男" android:checked="true"></RadioButton>
15 <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioFemale" android:text="女"></RadioButton>
16  </RadioGroup>
17 <TextView
18 android:id="@+id/tvSex"
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:text="您的性别是:男"
22 android:textSize="9pt"
23 />
24 </LinearLayout>

选中项变更的事件监听:

当RadioGroup中的选中项变更后,您可能需要做一些相应,比如上述例子中,性别选择“女”后下面的本文也相应改变,又或者选择不同的性别后,出现符合该性别的头像列表进行更新,女生不会喜欢使用大胡子作为自己的头像。

如果您对监听器不熟悉,可以阅读Android控件系列之Button以及Android监听器

后台代码如下:

1 TextView tv = null;//根据不同选项所要变更的文本控件
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5
6 setContentView(R.layout.main);
7
8 //根据ID找到该文本控件
9 tv = (TextView)this.findViewById(R.id.tvSex);
10 //根据ID找到RadioGroup实例
11 RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
12 //绑定一个匿名监听器
13 group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
14
15 @Override
16 public void onCheckedChanged(RadioGroup arg0, int arg1) {
17 // TODO Auto-generated method stub
18 //获取变更后的选中项的ID
19 int radioButtonId = arg0.getCheckedRadioButtonId();
20 //根据ID获取RadioButton的实例
21 RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
22 //更新文本内容,以符合选中项
23 tv.setText("您的性别是:" + rb.getText());
24 }
25 });
26 }

效果如下:

Android控件系列之RadioButton&amp;RadioGroup第2张

总结:

本文介绍了Android中如何使用RadioGroup和RadioButton,对比了RadioButton和CheckBox的区别,并实现了自定义的RadioGroup中被选中RadioButton的变更监听事件。

免责声明:文章转载自《Android控件系列之RadioButton&amp;amp;RadioGroup》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇luaFramework第15.36节 PyQt(Python+Qt)入门学习:containers容器类部件QFrame框架部件介绍下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

让x86的android模拟器能模拟arm架构系统

2019年展月6日更新: 1、最近发现了一个能在linux下模拟arm的模拟器: xdroid,网址: https://www.linzhuotech.com/index.php/home/index/down.html, 特别好用。直接模拟arm。我的云盘里有其可执行程序。 (这个需要 5G 的根目录空间,不推荐,太大了,这个是linux上运行,不是模拟...

Android学习之五:android一些基本控件

每一个GUI开发工具都会提供一些基本的控件,例如Label和Button 等,下面我们来看下Android的一些基本控件。 Label:就是只用来显示些文本信息,而且不需要编辑的控件,在Android中是使用TextView控件的。我们来看一下在xml文件下面怎么定义该控件,我们来看以下的xml代码: <TextView android:lay...

Android 内存管理介绍

极力推荐Android 开发大总结文章:欢迎收藏程序员Android 力荐 ,Android 开发者需要的必备技能 Android Runtime(ART)和Dalvik虚拟机使用 分页 和 内存映射 来管理内存。 这意味着应用程序修改的任何内存(无论是通过分配新对象通过映射页面)都将保留在RAM中,并且不能被分页。 应用程序释放内存的唯一方法是释...

Android关于API level、buildToolVersion、CompileSdkVersion

API level: API level是一个整数,它指的是我们使用的框架(Framework)的版本,也就是我们使用的sdk中的各个平台下的android.jar。 但是这个API level又和Android系统的版本有着对应关系,并且每个系统都会在内部记录它所使用的API level。 https://developer.android.com/gu...

.NET跨平台机制一(mono for android配置教程)

    忙完了毕业设计,坐等毕业,终于有时间可以玩玩.NET的跨平台机制了,当然了.NET跨平台主要就是Linux,所以就蛮有心思的去配置了下mono for android的开发环境。       首先,准备工作要做足,运行时,虚拟机,模拟器都要先下载好了。 前期准备,先看看安卓模拟器的配置(已经会配置的略过..)配置教程网络上也很多,我这里就稍微盖过...

Android_广播

BroadcastReceiver 一,概述 使用场景: 1.同一app内部的组件之间的消息通信 2.同一app不同进程之间的消息通信 3.不同app之间的组件之间消息通信 4.Android系统与app之间的消息通信 实现原理:观察者模式,基于消息的发布/订阅事件模型。 实现流程(大致): 1.接受者通过Binder机制向AMS(Activity Man...