Android基础教程(七)之----单选项框RadioGroup的综合应用

大家好,我们今天这一节要介绍的是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. <?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.   <TextView
  9.     android:id="@+id/myTextView"
  10.     android:layout_width="228px"
  11.     android:layout_height="49px"
  12.     android:text="@string/str_radio_question1"
  13.     android:textSize="30sp"  
  14.   />  
  15.   <!--建立一個RadioGroup -->
  16.   <RadioGroup
  17.     android:id="@+id/myRadioGroup"
  18.     android:layout_width="137px"
  19.     android:layout_height="216px"
  20.     android:orientation="vertical"
  21.     >
  22.     <!--第一個RadioButton -->
  23.     <RadioButton
  24.       android:id="@+id/myRadioButton1"
  25.       android:layout_width="wrap_content"
  26.       android:layout_height="wrap_content"
  27.       android:text="@string/tr_radio_op1"
  28.     />
  29.     <!--第二個RadioButton -->
  30.     <RadioButton
  31.       android:id="@+id/myRadioButton2"
  32.       android:layout_width="wrap_content"
  33.       android:layout_height="wrap_content"
  34.       android:text="@string/tr_radio_op2"
  35.     />
  36.     </RadioGroup>    
  37. </LinearLayout>
复制代码
最后是主控制程序RadioGroupDemo.java:
  1. package com.android.test;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.RadioButton;
  5. import android.widget.RadioGroup;
  6. import android.widget.TextView;

  7. public class RadioGroupDemo extends Activity
  8. {
  9.   public TextView mTextView1;
  10.   public RadioGroup mRadioGroup1;
  11.   public RadioButton mRadio1,mRadio2;

  12.   public void onCreate(Bundle savedInstanceState)
  13.   {
  14.     super.onCreate(savedInstanceState);
  15.     setContentView(R.layout.main);
  16.     
  17.     /*取得 TextView、RadioGroup、RadioButton对象*/
  18.     mTextView1 = (TextView) findViewById(R.id.myTextView);
  19.     mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
  20.     mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
  21.     mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
  22.     
  23.     /*RadioGroup用OnCheckedChangeListener来运行*/
  24.     mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
  25.   }
  26.   
  27.   private RadioGroup.OnCheckedChangeListener mChangeRadio = new
  28.           RadioGroup.OnCheckedChangeListener()
  29.   {
  30.     @Override
  31.     public void onCheckedChanged(RadioGroup group, int checkedId)
  32.     {
  33.       // TODO Auto-generated method stub
  34.       if(checkedId==mRadio1.getId())
  35.       {
  36.         /*把mRadio1的内容传到mTextView1*/
  37.         mTextView1.setText(mRadio1.getText());
  38.       }
  39.       else if(checkedId==mRadio2.getId())
  40.       {
  41.         /*把mRadio2的内容传到mTextView1*/
  42.         mTextView1.setText(mRadio2.getText());
  43.       }      
  44.     }
  45.   };
  46. }
复制代码
运行RadioGroupDemo.java ,将得到以上效果。

转载于:https://my.oschina.net/shootercn/blog/11879

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值