控件_RadioGroup&&RadioButton(单选按钮)和Toast

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context=".MainActivity" >
11 
12     <RadioGroup
13         android:id="@+id/genderGroup"
14         android:layout_width="wrap_content" 
15         android:layout_height="wrap_content" 
16         android:orientation="vertical"
17         >
18         <RadioButton
19             android:id="@+id/man"
20              android:layout_width="wrap_content" 
21               android:layout_height="wrap_content" 
22               android:text="男"
23             />
24         <RadioButton
25         android:id="@+id/woman"
26          android:layout_width="wrap_content" 
27           android:layout_height="wrap_content" 
28           android:text="女"
29           />
30         
31     </RadioGroup>
32 
33 </LinearLayout>
 1 import android.app.Activity;
 2 import android.os.Bundle;
 3 import android.widget.CompoundButton;
 4 import android.widget.RadioButton;
 5 import android.widget.RadioGroup;
 6 import android.widget.RadioGroup.OnCheckedChangeListener;
 7 import android.widget.Toast;
 8 
 9 public class MainActivity extends Activity {
10     private RadioGroup genderGroup;
11     private RadioButton man;
12     private RadioButton woman;
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16         
17         genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
18         man = (RadioButton) findViewById(R.id.man);
19         woman = (RadioButton) findViewById(R.id.woman);
20         
21         RadioGroupListener Grouplistener = new RadioGroupListener();
22         genderGroup.setOnCheckedChangeListener(Grouplistener);
23         RadioButtonListener Buttonlistener = new RadioButtonListener();
24         man.setOnCheckedChangeListener(Buttonlistener);
25         woman.setOnCheckedChangeListener(Buttonlistener);
26     }
27     
28     class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{
29         public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
30             System.out.println(isChecked);
31         }
32         
33     }
34     class RadioGroupListener implements OnCheckedChangeListener{
35         public void onCheckedChanged(RadioGroup group, int checkedId) {
36             if(man.getId()==checkedId){
37                 System.out.println("man");//这里不是简单的输出,要有针对的处理,比如更改数据库里的内容
38                 Toast.makeText(MainActivity.this, "man", Toast.LENGTH_SHORT).show();
39             }else if(woman.getId()==checkedId){
40                 System.out.println("woman");
41                 Toast.makeText(MainActivity.this, "woman", Toast.LENGTH_SHORT).show();
42             }
43         }
44         
45     }
46 }

 

 

 

RadioGroup和CheckBox

 1 import android.os.Bundle;
 2 import android.widget.CheckBox;
 3 import android.widget.CompoundButton;
 4 import android.widget.RadioButton;
 5 import android.widget.RadioGroup;
 6 import android.widget.Toast;
 7 import android.app.Activity;
 8 
 9 
10 public class Activity07 extends Activity {
11     //对控件对象进行声明
12     private RadioGroup genderGroup = null;
13     private RadioButton femaleButton = null;
14     private RadioButton maleButton = null;
15     private CheckBox swimBox = null;
16     private CheckBox runBox = null;
17     private CheckBox readBox = null;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.radio);
22         
23         //通过控件的ID来得到代表控件的对象
24         genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
25         femaleButton = (RadioButton)findViewById(R.id.femaleButton);
26         maleButton = (RadioButton)findViewById(R.id.maleButton);
27         swimBox = (CheckBox)findViewById(R.id.swim);
28         runBox = (CheckBox)findViewById(R.id.run);
29         readBox = (CheckBox)findViewById(R.id.read);
30         
31         //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同
32         genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
33             
34             @Override
35             public void onCheckedChanged(RadioGroup group, int checkedId) {//传进来的是控件的id
36                 if(femaleButton.getId() == checkedId){
37                     System.out.println("famale");//这里不是简单的输出,要有针对的处理,比如更改数据库里的内容
38                     Toast.makeText(Activity07.this,"famale", Toast.LENGTH_SHORT).show();//用于提示用户
39                 }
40                 else if(maleButton.getId() == checkedId){
41                     System.out.println("male");
42                     Toast.makeText(Activity07.this,"male", Toast.LENGTH_SHORT).show();//用于提示用户
43                 }
44                 
45             }
46         });
47         
48       //为多选按钮添加监听器
49         swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
50             @Override
51             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//传进来的是是否选中
52                 if(isChecked){
53                     System.out.println("swim is checked");
54                 }else{
55                     System.out.println("swim is unchecked");
56                 }
57                 
58             }
59         });
60         
61         runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
62             @Override
63             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
64                 if(isChecked){
65                     System.out.println("run is checked");
66                 }else{
67                     System.out.println("run is unchecked");
68                 }
69                 
70             }
71         });
72         
73         readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
74             @Override
75             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
76                 if(isChecked){
77                     System.out.println("read is checked");
78                 }else{
79                     System.out.println("read is unchecked");
80                 }
81                 
82             }
83         });
84     }
85 }

 

 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 <RadioGroup
 8     android:id="@+id/genderGroup"
 9     android:layout_width="wrap_content" 
10     android:layout_height="wrap_content" 
11     android:orientation="vertical"
12     >
13     <RadioButton
14         android:id="@+id/femaleButton"
15          android:layout_width="wrap_content" 
16           android:layout_height="wrap_content" 
17           android:text="@string/female"
18           />
19     <RadioButton
20         android:id="@+id/maleButton"
21          android:layout_width="wrap_content" 
22           android:layout_height="wrap_content" 
23           android:text="@string/male"
24           />
25 </RadioGroup>
26 
27 
28 <CheckBox
29     android:id="@+id/swim"
30      android:layout_width="wrap_content" 
31       android:layout_height="wrap_content" 
32       android:text="@string/swim"
33       />
34 <CheckBox
35     android:id="@+id/run"
36      android:layout_width="wrap_content" 
37       android:layout_height="wrap_content" 
38       android:text="@string/run"
39       />
40 <CheckBox
41     android:id="@+id/read"
42      android:layout_width="wrap_content" 
43       android:layout_height="wrap_content" 
44       android:text="@string/read"
45       />
46 </LinearLayout>

 

posted @ 2014-03-06 16:59  胡椒粉hjf  阅读(458)  评论(0编辑  收藏  举报