RadioGroupRadioButton
RadioGroupRadioButtonActivity:
1 package com.vanceinfo.RadioGroupRadioButton;
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.Toast;
8 import android.widget.RadioGroup.OnCheckedChangeListener;
9 import android.widget.TextView;
10
11 public class RadioGroupRadioButton extends Activity
12 {
13 private RadioGroup mRadioGroup;
14 private TextView mTextView;
15 private RadioButton mRadioButton01, mRadioButton02, mRadioButton03, mRadioButton04;
16
17 @Override
18 public void onCreate(Bundle savedInstanceState)
19 {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22 mTextView = (TextView) findViewById(R.id.textView);
23 mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
24 mRadioButton01 = (RadioButton) findViewById(R.id.readioButton01);
25 mRadioButton02 = (RadioButton) findViewById(R.id.readioButton02);
26 mRadioButton03 = (RadioButton) findViewById(R.id.readioButton03);
27 mRadioButton04 = (RadioButton) findViewById(R.id.readioButton04);
28 mRadioGroup.setOnCheckedChangeListener(listener);
29 }
30
31 // 设置监听
32 OnCheckedChangeListener listener = new OnCheckedChangeListener()
33 {
34 @Override
35 public void onCheckedChanged(RadioGroup group, int checkedId)
36 {
37 if (checkedId == mRadioButton03.getId())
38 {
39 Display("正确答案是:" + mRadioButton03.getText() + ",恭喜你!回答正确");
40 }
41 else
42 {
43 Display("抱歉!回答错误");
44 }
45 }
46 };
47
48 public void Display(String str)
49 {
50 Toast.makeText(this, str, Toast.LENGTH_LONG).show();
51 }
52 }
AndroidManifest/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" android:layout_width="fill_parent"
4 android:layout_height="fill_parent">
5 <TextView
6 android:id="@+id/textView"
7 android:layout_width="fill_parent"
8 android:layout_height="wrap_content" android:text="@string/hello" />
9 <RadioGroup
10 android:id="@+id/radioGroup"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:orientation="vertical"
14 android:layout_x="3px"
15 android:layout_y="54px"
16 >
17 <RadioButton
18 android:id="@+id/readioButton01"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content"
21 android:text="@string/Macos"
22 />
23 <RadioButton
24 android:id="@+id/readioButton02"
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:text="@string/java"
28 />
29 <RadioButton
30 android:id="@+id/readioButton03"
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:text="@string/linux"
34 />
35 <RadioButton
36 android:id="@+id/readioButton04"
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:text="@string/windows"
40 />
41 </RadioGroup>
42 </LinearLayout>