第20章、OnCheckedChangeListener事件(从零开始学Android)

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

一、布局

  1、打开“res/layout/activity_main.xml”文件。

[html]  view plain copy
  1. <RelativeLayout   
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <RadioGroup  
  9.         android:id="@+id/gender"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true" >  
  14.         <RadioButton  
  15.             android:id="@+id/male"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:checked="true"  
  19.             android:text="男" />  
  20.         <RadioButton  
  21.             android:id="@+id/female"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="女" />  
  25.     RadioGroup>  
  26.   
  27.     <CheckBox  
  28.         android:id="@+id/football"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_alignParentLeft="true"  
  32.         android:layout_below="@+id/gender"  
  33.         android:text="足球" />  
  34.     <CheckBox  
  35.         android:id="@+id/basketball"  
  36.         android:layout_width="wrap_content"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_alignParentLeft="true"  
  39.         android:layout_below="@+id/football"  
  40.         android:text="蓝球" />  
  41.   
  42. RelativeLayout>  

  2、界面如下:

  

 

二、OnCheckedChangeListener事件 

  打开“src/com.genwoxue.oncheckedchanged/MainActivity.java”文件。

  然后输入以下代码:  

[java]  view plain copy
  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事件,但注意二者区别。

  效果如下:

  

你可能感兴趣的:(软件)