单项选择功能相信大家都不陌生,平时我们做网上调查,网上问卷的时候就遇到了不少,在Android中,我们使用RadioGroup和RadioButton可以同样具备这样的功能,我们通过下面的示例来演示下Android中的单项选择功能的使用,示例会让用户作出一个艰难的决定,根据用户选择的答案,来显示正确与否,这个显示就用我们在帮助提示工具Toast的简单使用一文中提到的toast。
动作一:
创建RadioTest工程,我完善之后的工程目录如下:
动作二:
我们在main.xml文件中添加一个RadioGroup,并在RadioGroup中添加4个选项,当然这4个选项是用RadioButton来实现的,每一个RadioButton我们让其来显示对应的选项答案,完整的main.xml内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <RadioGroup
- android:id="@+id/myRadioGroup"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:orientation="vertical"
- android:layout_x="3px"
- android:layout_y="54px">
- <RadioButton
- android:id="@+id/RadioButton1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/RadioButton1" />
- <RadioButton
- android:id="@+id/RadioButton2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/RadioButton2" />
- <RadioButton
- android:id="@+id/RadioButton3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/RadioButton3" />
- <RadioButton
- android:id="@+id/RadioButton4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/RadioButton4" />
- </RadioGroup>
- </LinearLayout>
动作三:
既然我们需要让每一个RadioButton来显示对应的选项内容,那我们就需要在values目录下添加一些字符串信息了,完整的strings.xml内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">RadioTest</string>
- <string name="hello">你是怎么知道OurAndroid这个Android开发分享网站的?</string>
- <string name="RadioButton1">搜索引擎搜索到的;</string>
- <string name="RadioButton2">从主站OurUnix传送过来的;</string>
- <string name="RadioButton3">直接输入域名http://android.ourunix.org;</string>
- <string name="RadioButton4">瞎闯进来的。</string>
- </resources>
动作四:
上面几步都完成之后,我们看最主要的RadioTestActivity.java文件里面的内容。在最后通过setOnCheckedChangeListener方法来监听用户做的是什么选择,并根据选择显示是否为正确答案。
完整内容及注释请看下面:
- package org.ourunix.android.radiotest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class RadioTestActivity extends Activity {
- // 定义一个RadioGroup和四个RadioButton
- private RadioGroup mRadioGroup;
- private RadioButton mRadioButton1;
- private RadioButton mRadioButton2;
- private RadioButton mRadioButton3;
- private RadioButton mRadioButton4;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 获取main.xml中的RadioGroup和RadioButton
- mRadioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
- mRadioButton1 = (RadioButton) findViewById(R.id.RadioButton1);
- mRadioButton2 = (RadioButton) findViewById(R.id.RadioButton2);
- mRadioButton3 = (RadioButton) findViewById(R.id.RadioButton3);
- mRadioButton4 = (RadioButton) findViewById(R.id.RadioButton4);
- // 这边主要用来设置RadioGroup的监听的,并对用户的选择作出判断
- mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if (checkedId == mRadioButton2.getId()) {
- display("答对!");
- } else {
- display("错误!");
- }
- }
- });
- }
- // 用Toast来显示正确与否的信息
- public void display(String str) {
- Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
- toast.setGravity(Gravity.TOP, 0, 450);
- toast.show();
- }
- }
动作五:
运行RadioTest,效果如下:
用户选择错误答案时
用户选择正确答案时
源码:RadioTest