大家好,我们今天这一节要介绍的是RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只能做出单一选择(单选题).

首先,我们先设计一个TextView Widget ,以及一个RadioGroup ,并将该RadioGroup 内放置两个RadioButton ,默认为都不选择,在程序运行阶段,利用onCheckedChanged 作为启动事件装置,让User选择其中一个按钮,显示被选择的内容,最的将RadioButton 的选项文字显示于TextView 当中.

下面我们看一下效果图:

    

下面是涉及的相关代码:

string.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, RadioGroupDemo</string>
  4. <string name="app_name">RadioGroupDemo</string>
  5. <string name="tr_radio_op1">帅哥</string>
  6. <string name="tr_radio_op2">美女</string>
  7. <string name="str_radio_question1">请问你是?</string>
  8. </resources>

主布局main.xml:

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <!--第一個TextView -->
  9. <TextView
  10. android:id="@+id/myTextView"
  11. android:layout_width="228px"
  12. android:layout_height="49px"
  13. android:text="@string/str_radio_question1"
  14. android:textSize="30sp"
  15. />
  16. <!--建立一個RadioGroup -->
  17. <RadioGroup
  18. android:id="@+id/myRadioGroup"
  19. android:layout_width="137px"
  20. android:layout_height="216px"
  21. android:orientation="vertical"
  22. >
  23. <!--第一個RadioButton -->
  24. <RadioButton
  25. android:id="@+id/myRadioButton1"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="@string/tr_radio_op1"
  29. />
  30. <!--第二個RadioButton -->
  31. <RadioButton
  32. android:id="@+id/myRadioButton2"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="@string/tr_radio_op2"
  36. />
  37. </RadioGroup>
  38. </LinearLayout>

最后是主控制程序RadioGroupDemo.java:

  1. package com.android.test;
  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. public class RadioGroupDemo extends Activity
  10. {
  11. public TextView mTextView1;
  12. public RadioGroup mRadioGroup1;
  13. public RadioButton mRadio1,mRadio2;
  14.  
  15. public void onCreate(Bundle savedInstanceState)
  16. {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19.  
  20. /*取得 TextView、RadioGroup、RadioButton对象*/
  21. mTextView1 = (TextView) findViewById(R.id.myTextView);
  22. mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
  23. mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
  24. mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
  25.  
  26. /*RadioGroup用OnCheckedChangeListener来运行*/
  27. mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
  28. }
  29.  
  30. private RadioGroup.OnCheckedChangeListener mChangeRadio = new
  31. RadioGroup.OnCheckedChangeListener()
  32. {
  33. @Override
  34. public void onCheckedChanged(RadioGroup group, int checkedId)
  35. {
  36. // TODO Auto-generated method stub
  37. if(checkedId==mRadio1.getId())
  38. {
  39. /*把mRadio1的内容传到mTextView1*/
  40. mTextView1.setText(mRadio1.getText());
  41. }
  42. else if(checkedId==mRadio2.getId())
  43. {
  44. /*把mRadio2的内容传到mTextView1*/
  45. mTextView1.setText(mRadio2.getText());
  46. }
  47. }
  48. };
  49. }

运行RadioGroupDemo.java ,将得到以上效果...今天就到这~

android.widget.RadioButton 单选按钮(转)的更多相关文章

  1. Android控件-单选按钮RadioButton

    RadioGroup单选按钮用法,还是先看效果图 先中后,点RadioGroup测试按钮,可在标题栏显示选择结果,点清除可以清除选择.下面上代码,main.xml: <RadioGroup an ...

  2. 03 RadioButton 单选按钮

    >概念:从多个互斥选项中选择一个  如果是选项全部展开  RadioButton  不是展开的Spinner(下拉列表)     >属性: android:checked="tr ...

  3. 控件_RadioGroup&&RadioButton(单选按钮)和Toast

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  4. Android之RadioButton多行

    RadioGroup设置orientation="vertical"竖向单列显示 RadioGroup设置orientation="horizontal"横向单 ...

  5. android开发之单选按钮

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  6. Android设置RadioButton在文字的右边

    效果图如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  7. Android 自定义RadioButton实现

    由于使用小米系统MIUI运行是RadioButton样式跟google Android API自定义的不一样,则我们可以定义任何想要的东东.没有做不到,只有想不到 Android 自定义RadioBu ...

  8. 关于java.lang.NoSuchMethodError: android.widget.RelativeLayout.setBackground的解决办法

    今天用一个安卓4.0.4版本的手机测试手上的项目,发现logcat弹出这样一个提示“java.lang.NoSuchMethodError: android.widget.RelativeLayout ...

  9. java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ProgressBar$SavedState

    java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.Progress ...

随机推荐

  1. mac word 快捷键

      https://support.office.com/en-us/article/Word-keyboard-shortcuts-c0ca851f-3d58-4ce0-9867-799df7366 ...

  2. ODAC(V9.5.15) 学习笔记(十六)直接访问模式

    直接访问模式(Direct mode)是ODAC最大的特色之一,即不需要安装Oracle客户端,ODAC越过了OCI(Oracle Call Interface ),使用TCP/IP协议就可以直接与O ...

  3. 重构第3天:方法提公(Pull Up Method)

    理解:方法提公,或者说把方法提到基类中. 详解:如果大于一个继承类都要用到同一个方法,那么我们就可以把这个方法提出来放到基类中.这样不仅减少代码量,而且提高了代码的重用性. 看重构前的代码: usin ...

  4. 一起做RGB-D SLAM 第二季 (一)

    小萝卜:师兄!过年啦!是不是很无聊啊!普通人的生活就是赚钱花钱,实在是很没意思啊! 师兄:是啊…… 小萝卜:他们都不懂搞科研和码代码的乐趣呀! 师兄:可不是嘛…… 小萝卜:所以今年过年,我们再做一个S ...

  5. CentOS下Redis服务器安装配置

    说明: 操作系统:CentOS 1.安装编译工具 yum install wget  make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-de ...

  6. jQuery手机端上拉刷新下拉加载更多页面

    基于jQuery手机端上拉下拉刷新页面代码.这是一款类似QQ空间客户端或者微信下拉刷新页面特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id=" ...

  7. 充实你的素材库!10款免费的 PSD 素材下载

    由于网页设计师没有时间来自己从零开始设计,所以在设计项目中使用网络上已有的设计素材是常见的方式.这就是为什么我们经常会到网上搜索可以免费下载的素材. 今天,我们这里有几套不同的免费的 PSD 素材分享 ...

  8. LeetCode—— Median of Two Sorted Arrays

    Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...

  9. 探秘重编译(Recompilations)(1/2)

    这篇文章我想谈下SQL Server里一个非常重要的性能调优话题:重编译(Recompilations) .当你执行非常简单的存储过程(使用临时表)时,就会发生.今天我想奠定SQL Server里重编 ...

  10. 内存中OLTP(Hekaton)里的事务日志记录

    在今天的文章里,我想详细讨论下内存中OLTP里的事务日志如何写入事务日志.我们都知道,对于你的内存优化表(Memory Optimized Tables),内存中OLTP提供你2个持久性(durabi ...