学习目的:

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

选中项变更的事件监听:

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

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

后台代码如下:

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

    1. 在线安装 yum install memcached: 源代码安装 wget http://memcached.org/latest 下载最新版本 tar -zxvf memcached-1.x ...

  2. Jquery 获取URL参数

    使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下 1.window.location.href; 其实只是用到了javas ...

  3. Linux命令sed

    如果一个文本文件数据比较多,大概有40万条数据,我想取出第500-1000条数据,保存到另一个文件,用linux命令该如何操作? sed -n '500,1000p' 41w.txt > new ...

  4. Grunt之项目脚手架

    在网上搜了下,grunt这方面的教程挺少的,来去都是一些被频繁转载的文章.唉,人艰不拆啊. 首先我们在全局环境中安装grunt-init. npm install -g grunt-init 来看看官 ...

  5. ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式)

    最近做项目遇到生成二维码的问题,发现网上用的最多的是ThoughtWorks.QRCode和QrCode.Net两种方式.访问官网看着例子写了两个Demo,使用过程中发现两个都挺好用的,Thought ...

  6. Sublime text 3 中文文件名显示方框怎么解决?

    如图,中文文件名打开全是乱码,内容倒是装了converttoutf8没什么太大的问题. -------------------------------------------------------- ...

  7. Zhulina 的高分子刷理论

    高分子刷的解析平均场理论有两种表述方式.一个是MWC理论(Macromolecules 1988, 21, 2610-2619),另外一个就是Zhulina和Birshtein这两位俄罗斯老太太的理论 ...

  8. BI就是报表?

    实际上,报表只是BI的一部分,虽然BI应用的结果通常需要通过报表来展示,但是,BI绝对不仅仅是报表.其实,大家对这些概念的理解,如同15年前的ERP一样.1998年,国内两大巨头金蝶与用友都开始宣称从 ...

  9. 在生产环境使用Docker部署应用

    导读 Docker现在越来越流行,但是真正在生产环境部署Docker还是个比较新的概念,还没有一个标准的流程.作者是ROR的程序员,作者结合平时的部署经验,联系Docker的特点,向大家分享了其在生产 ...

  10. vundle安装 给vim插上翅膀

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) vundle安装方法如下 首先执行以下命令 $ git clone https://githu ...