学习目的:

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

2、掌握RadioGroup的常用属性

3、理解RadioButton和CheckBox的区别

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

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中如何使用RadioGroup和RadioButton,对比了RadioButton和CheckBox的区别,并实现了自定义的RadioGroup中被选中RadioButton的变更监听事件。

摘自:http://www.cnblogs.com/wt616/archive/2011/06/20/2085531.html

Android控件系列之RadioButton&RadioGroup(转)的更多相关文章

  1. Android控件系列之RadioButton&RadioGroup

    学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...

  2. Android控件系列之CheckBox

    学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...

  3. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  4. Android控件介绍

    1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...

  5. 一步一步学android控件(之十六)—— CheckBox

    根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...

  6. Android控件TextView的实现原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8636153 在前面一个系列的文章中,我们以窗口 ...

  7. Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)

    本人之前以前撰文描写叙述Appium和UIAutomator框架是怎样定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议 Appium基于安卓的各种Fin ...

  8. Robotium之Android控件定位实践和建议

    本人之前曾经撰文描述Appium和UIAutomator框架是如何定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议Appium基于安卓的各种FindEl ...

  9. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

随机推荐

  1. PHP大批量插入数据库的3种方法和速度对比

    第一种,使用insert into 插入,最后显示为:23:25:05 01:32:05 也就是花了2个小时多! $params = array(‘value'=>'50′); set_time ...

  2. table设置滚动条

    .table {display: block;height:300px;overflow-y:auto;} 在bootstrap的Modal中使用此设置,可能会父容器溢出,但不会显示出来,会在页面侧边 ...

  3. 如何用jar命令对java工程进行打包

    如何用jar命令对java工程进行打包 有时候为了更方便快捷的部署和执行Java程序,要把java应用程序打包成一个jar包.而这个基础的操作有时候也很麻烦,为了方便java程序员们能够方便的打包ja ...

  4. stripslashes — 反引用一个引用字符串

    stripslashes (PHP 4, PHP 5) stripslashes — 反引用一个引用字符串 Report a bug  说明 string stripslashes ( string  ...

  5. zstu.4189: 逻辑运算(构建 && 前缀表达式入门)

    4189: 逻辑运算 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 274  Solved: 42 Description 还记得大学里学过的模电么, ...

  6. Common Subsequence(dp)

    Common Subsequence Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 951  Solved: 374 Description A subs ...

  7. 初识suse-Linux相关!

    Linux这种系统很奇怪,差不多每种不同的版本,它所使用的安装等一些重要命令皆有所变化.假若,你要熟练掌握一种OS,那么如果安装软件/应用,那是入门的第一步. 安装命令中: RedHat.CentOS ...

  8. UIImageView 动画 / UIImage 方向

    UIImage 方向 UIImage imageOrientation是相对当前屏幕的横竖屏来判断方向 如果本身是横屏, 照片也是横屏的话, 方向是正方向 BOOL b1 = (originalIma ...

  9. Dynamic Morphing Square(动态变形矩阵)

    题目描述: 解题思路: 先对输入的N进行判断,是否不小于3,如果小于3,需要继续输入一个新的数,知道输入的N比3大. 第一个打印的矩阵,*号为最外面一圈,其余全为-. 第二个打印的矩阵,*号向内缩减了 ...

  10. dex和odex相互转换

    一.dex和odex dex是安卓dalvik虚拟机的可执行文件,可以在导出的apk文件里用解压缩软件直接打开.odex是经过优化过的dex.odex一种是从apk程序中提取出来的,与apk文件存放在 ...