单项选择功能RadioGroup和RadioButton的使用

单项选择功能相信大家都不陌生,平时我们做网上调查,网上问卷的时候就遇到了不少,在Android中,我们使用RadioGroup和RadioButton可以同样具备这样的功能,我们通过下面的示例来演示下Android中的单项选择功能的使用,示例会让用户作出一个艰难的决定,根据用户选择的答案,来显示正确与否,这个显示就用我们在帮助提示工具Toast的简单使用一文中提到的toast。

动作一:

创建RadioTest工程,我完善之后的工程目录如下:

 RadioTest工程目录

动作二:

我们在main.xml文件中添加一个RadioGroup,并在RadioGroup中添加4个选项,当然这4个选项是用RadioButton来实现的,每一个RadioButton我们让其来显示对应的选项答案,完整的main.xml内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     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.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.     <RadioGroup  
  12.         android:id="@+id/myRadioGroup"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_width="fill_parent"  
  15.         android:orientation="vertical"  
  16.         android:layout_x="3px"  
  17.         android:layout_y="54px">  
  18.         <RadioButton  
  19.             android:id="@+id/RadioButton1"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:text="@string/RadioButton1" />  
  23.         <RadioButton  
  24.             android:id="@+id/RadioButton2"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="@string/RadioButton2" />  
  28.         <RadioButton  
  29.             android:id="@+id/RadioButton3"  
  30.             android:layout_width="fill_parent"  
  31.             android:layout_height="wrap_content"  
  32.             android:text="@string/RadioButton3" />  
  33.         <RadioButton  
  34.             android:id="@+id/RadioButton4"  
  35.             android:layout_width="fill_parent"  
  36.             android:layout_height="wrap_content"  
  37.             android:text="@string/RadioButton4" />  
  38.     </RadioGroup>  
  39. </LinearLayout>

动作三:

既然我们需要让每一个RadioButton来显示对应的选项内容,那我们就需要在values目录下添加一些字符串信息了,完整的strings.xml内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">RadioTest</string>  
  4.     <string name="hello">你是怎么知道OurAndroid这个Android开发分享网站的?</string>  
  5.     <string name="RadioButton1">搜索引擎搜索到的;</string>  
  6.     <string name="RadioButton2">从主站OurUnix传送过来的;</string>  
  7.     <string name="RadioButton3">直接输入域名http://android.ourunix.org;</string>  
  8.     <string name="RadioButton4">瞎闯进来的。</string>  
  9. </resources> 

动作四:

上面几步都完成之后,我们看最主要的RadioTestActivity.java文件里面的内容。在最后通过setOnCheckedChangeListener方法来监听用户做的是什么选择,并根据选择显示是否为正确答案。

完整内容及注释请看下面:

  1. package org.ourunix.android.radiotest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.widget.RadioButton;  
  7. import android.widget.RadioGroup;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10. import android.widget.RadioGroup.OnCheckedChangeListener;  
  11.   
  12. public class RadioTestActivity extends Activity {  
  13.     // 定义一个RadioGroup和四个RadioButton  
  14.     private RadioGroup mRadioGroup;  
  15.     private RadioButton mRadioButton1;  
  16.     private RadioButton mRadioButton2;  
  17.     private RadioButton mRadioButton3;  
  18.     private RadioButton mRadioButton4;  
  19.   
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         // 获取main.xml中的RadioGroup和RadioButton  
  26.         mRadioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);  
  27.         mRadioButton1 = (RadioButton) findViewById(R.id.RadioButton1);  
  28.         mRadioButton2 = (RadioButton) findViewById(R.id.RadioButton2);  
  29.         mRadioButton3 = (RadioButton) findViewById(R.id.RadioButton3);  
  30.         mRadioButton4 = (RadioButton) findViewById(R.id.RadioButton4);  
  31.         // 这边主要用来设置RadioGroup的监听的,并对用户的选择作出判断  
  32.         mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  33.   
  34.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  35.                 // TODO Auto-generated method stub  
  36.                 if (checkedId == mRadioButton2.getId()) {  
  37.                     display("答对!");  
  38.                 } else {  
  39.                     display("错误!");  
  40.                 }  
  41.   
  42.             }  
  43.         });  
  44.   
  45.     }  
  46.   
  47.     // 用Toast来显示正确与否的信息  
  48.     public void display(String str) {  
  49.         Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);  
  50.         toast.setGravity(Gravity.TOP, 0, 450);  
  51.         toast.show();  
  52.     }  
  53.   
  54. }  

动作五:

运行RadioTest,效果如下:

 运行RadioTest效果图

用户选择错误答案时

用户选择错误答案

用户选择正确答案时

用户选择的正确答案

源码:RadioTest

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值