android 中单选和复选框监听操作

单选按钮RadioGroup、复选框CheckBox都有OnCheckedChangeListener事件,我们一起了解一下。

 

 1 package com.genwoxue.oncheckedchanged;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.widget.RadioGroup;
 6 import android.widget.RadioButton;
 7 import android.widget.RadioGroup.OnCheckedChangeListener;              //引入OnCheckedChangeListener事件相关包
 8 import android.widget.CheckBox;
 9 import android.widget.CompoundButton;
10 import android.widget.Toast;
11 
12 
13 public class MainActivity extends Activity {
14     private RadioGroup GenderGroup=null;
15     private RadioButton rbMale=null;
16     private RadioButton rbFemale=null;
17     private CheckBox cbFootBall=null;
18     private CheckBox cbBasketBall=null;
19     
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         
25         GenderGroup=(RadioGroup)super.findViewById(R.id.gender);
26         rbMale=(RadioButton)super.findViewById(R.id.male);
27         rbFemale=(RadioButton)super.findViewById(R.id.female);
28         cbFootBall=(CheckBox)super.findViewById(R.id.football);
29         cbBasketBall=(CheckBox)super.findViewById(R.id.basketball);
30         //在GenderGroup注册OnCheckedChangeListener事件
31                   GenderGroup.setOnCheckedChangeListener(new GenderOnCheckedChangeListener());
32                   //在cbFootBall注册OnCheckedChangeListener事件
33         cbFootBall.setOnCheckedChangeListener(new BootBallOnCheckedChangeListener());
34                   //在cbBasketBall注册OnCheckedChangeListener事件
35 
36         cbBasketBall.setOnCheckedChangeListener(new BasketBallOnCheckedChangeListener());
37     }
38     
39     private class GenderOnCheckedChangeListener implements OnCheckedChangeListener{
40         @Override
41         public void onCheckedChanged(RadioGroup group,int checkedId){
42             String sGender="";
43             if(rbFemale.getId()==checkedId){
44                 sGender=rbFemale.getText().toString();
45             }
46             if(rbMale.getId()==checkedId){
47                 sGender=rbMale.getText().toString();
48             }
49             Toast.makeText(getApplicationContext(), "您选择的性别是:"+sGender, Toast.LENGTH_LONG).show();
50         }
51         
52     }
53     
54     private class BootBallOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{
55         @Override
56         public void onCheckedChanged(CompoundButton button, boolean isChecked){
57             String sFav="";
58             if(isChecked){
59                 sFav=cbFootBall.getText().toString();
60                 sFav=sFav+"选中!";
61             }
62             else
63                 sFav=sFav+"未迁中";
64             Toast.makeText(getApplicationContext(), "您选择的爱好是:"+sFav, Toast.LENGTH_LONG).show();
65         }
66     }
67     
68     private class BasketBallOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{
69         @Override
70         public void onCheckedChanged(CompoundButton button,boolean isChecked){
71             String sFav="";
72             if(cbBasketBall.isChecked()){
73                 sFav=cbBasketBall.getText().toString();
74                 sFav=sFav+"选中!";
75             }
76             else
77                 sFav=sFav+"未迁中";
78             Toast.makeText(getApplicationContext(), "您选择的爱好是:"+sFav, Toast.LENGTH_LONG).show();
79         }
80     }
81     
82 }

管单选按钮和复选框都有OnCheckedChange事件,但注意二者区别。

  效果如下:

  

posted on 2014-07-04 11:34  lgphp  阅读(1893)  评论(0编辑  收藏  举报