第二个程序也非常简单,先看看效果吧,主要是演示如何使用RadioButton和RadioGroup
当你选择不同的品牌的时候会有不平的评论,呵呵。
源码如下:
1 package com.demo;
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 import android.widget.RadioGroup.OnCheckedChangeListener;
9
10 public class Demo02 extends Activity {
11
12 // 定义四个RadioButtom
13 RadioButton optionBenz;
14 RadioButton optionBMW;
15 RadioButton optionAudi;
16 RadioButton optionAlto;
17
18 // 定义评论的文本
19 TextView tvSummary;
20
21 /** Called when the activity is first created. */
22 @Override
23 public void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.main);
26
27 optionBenz = (RadioButton) this.findViewById(R.id.optBenz);
28 optionBMW = (RadioButton) this.findViewById(R.id.optBMW);
29 optionAudi = (RadioButton) this.findViewById(R.id.optAudi);
30 optionAlto = (RadioButton) this.findViewById(R.id.optAlto);
31 tvSummary = (TextView) this.findViewById(R.id.lbSummary);
32
33 // 为RadioGroup注册OnCheckedChangeListener事件的处理类
34 RadioGroup rgpAnswer = (RadioGroup) this.findViewById(R.id.rgpAnswer);
35 rgpAnswer.setOnCheckedChangeListener(OnRadioButtonClick);
36 }
37
38 private OnCheckedChangeListener OnRadioButtonClick = new OnCheckedChangeListener()
39 {
40 // 当选择不同的选项时,会给出不同的评语
41 public void onCheckedChanged(RadioGroup group, int checkedId)
42 {
43 if(checkedId == optionBenz.getId())
44 {
45 tvSummary.setText("哇,真有钱");
46 }
47 if(checkedId == optionBMW.getId())
48 {
49 tvSummary.setText("嗯,有品位");
50 }
51 if(checkedId == optionAudi.getId())
52 {
53 tvSummary.setText("嗯,有深度");
54 }
55 if(checkedId == optionAlto.getId())
56 {
57 tvSummary.setText("哎,没追求");
58 }
59 }
60 };
61 }
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 import android.widget.RadioGroup.OnCheckedChangeListener;
9
10 public class Demo02 extends Activity {
11
12 // 定义四个RadioButtom
13 RadioButton optionBenz;
14 RadioButton optionBMW;
15 RadioButton optionAudi;
16 RadioButton optionAlto;
17
18 // 定义评论的文本
19 TextView tvSummary;
20
21 /** Called when the activity is first created. */
22 @Override
23 public void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.main);
26
27 optionBenz = (RadioButton) this.findViewById(R.id.optBenz);
28 optionBMW = (RadioButton) this.findViewById(R.id.optBMW);
29 optionAudi = (RadioButton) this.findViewById(R.id.optAudi);
30 optionAlto = (RadioButton) this.findViewById(R.id.optAlto);
31 tvSummary = (TextView) this.findViewById(R.id.lbSummary);
32
33 // 为RadioGroup注册OnCheckedChangeListener事件的处理类
34 RadioGroup rgpAnswer = (RadioGroup) this.findViewById(R.id.rgpAnswer);
35 rgpAnswer.setOnCheckedChangeListener(OnRadioButtonClick);
36 }
37
38 private OnCheckedChangeListener OnRadioButtonClick = new OnCheckedChangeListener()
39 {
40 // 当选择不同的选项时,会给出不同的评语
41 public void onCheckedChanged(RadioGroup group, int checkedId)
42 {
43 if(checkedId == optionBenz.getId())
44 {
45 tvSummary.setText("哇,真有钱");
46 }
47 if(checkedId == optionBMW.getId())
48 {
49 tvSummary.setText("嗯,有品位");
50 }
51 if(checkedId == optionAudi.getId())
52 {
53 tvSummary.setText("嗯,有深度");
54 }
55 if(checkedId == optionAlto.getId())
56 {
57 tvSummary.setText("哎,没追求");
58 }
59 }
60 };
61 }