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

一、布局

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

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <RadioGroup        android:id="@+id/gender"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true" >        <RadioButton            android:id="@+id/male"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="男" />        <RadioButton            android:id="@+id/female"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="女" />    </RadioGroup>    <CheckBox        android:id="@+id/football"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/gender"        android:text="足球" />    <CheckBox        android:id="@+id/basketball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/football"        android:text="蓝球" /></RelativeLayout>

  2、界面如下:

  

二、OnCheckedChangeListener事件 

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

  然后输入以下代码:  

package com.genwoxue.oncheckedchanged;import android.os.Bundle;import android.app.Activity;import android.widget.RadioGroup;import android.widget.RadioButton;import android.widget.RadioGroup.OnCheckedChangeListener;              //引入OnCheckedChangeListener事件相关包import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.Toast;public class MainActivity extends Activity {private RadioGroup GenderGroup=null;private RadioButton rbMale=null;private RadioButton rbFemale=null;private CheckBox cbFootBall=null;private CheckBox cbBasketBall=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);GenderGroup=(RadioGroup)super.findViewById(R.id.gender);rbMale=(RadioButton)super.findViewById(R.id.male);rbFemale=(RadioButton)super.findViewById(R.id.female);cbFootBall=(CheckBox)super.findViewById(R.id.football);cbBasketBall=(CheckBox)super.findViewById(R.id.basketball);//在GenderGroup注册OnCheckedChangeListener事件                  GenderGroup.setOnCheckedChangeListener(new GenderOnCheckedChangeListener());                  //在cbFootBall注册OnCheckedChangeListener事件cbFootBall.setOnCheckedChangeListener(new BootBallOnCheckedChangeListener());                  //在cbBasketBall注册OnCheckedChangeListener事件cbBasketBall.setOnCheckedChangeListener(new BasketBallOnCheckedChangeListener());}private class GenderOnCheckedChangeListener implements OnCheckedChangeListener{@Overridepublic void onCheckedChanged(RadioGroup group,int checkedId){String sGender="";if(rbFemale.getId()==checkedId){sGender=rbFemale.getText().toString();}if(rbMale.getId()==checkedId){sGender=rbMale.getText().toString();}Toast.makeText(getApplicationContext(), "您选择的性别是:"+sGender, Toast.LENGTH_LONG).show();}}private class BootBallOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{@Overridepublic void onCheckedChanged(CompoundButton button, boolean isChecked){String sFav="";if(isChecked){sFav=cbFootBall.getText().toString();sFav=sFav+"选中!";}elsesFav=sFav+"未迁中";Toast.makeText(getApplicationContext(), "您选择的爱好是:"+sFav, Toast.LENGTH_LONG).show();}}private class BasketBallOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{@Overridepublic void onCheckedChanged(CompoundButton button,boolean isChecked){String sFav="";if(cbBasketBall.isChecked()){sFav=cbBasketBall.getText().toString();sFav=sFav+"选中!";}elsesFav=sFav+"未迁中";Toast.makeText(getApplicationContext(), "您选择的爱好是:"+sFav, Toast.LENGTH_LONG).show();}}}

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

  效果如下:

  

更多相关文章

  1. Android实现使用流媒体播放远程mp3文件的方法
  2. Android(安卓)Runtime
  3. android 自定义按钮样式
  4. 2020最新版Android一步一步教轻松通过ArcSoft虹软平台实现人脸识
  5. 手把手教你怎么去除Android(安卓)APP里面的广告
  6. Android零基础入门第35节:Android中基于回调的事件处理
  7. Android内部存储和外部存储的几个概念(内部存储空间和外部存储空
  8. Android(java方法)上实现mp4的分割和拼接 (二)
  9. [日更-2019.5.21] Android(安卓)系统的分区和文件系统(一)--Androi

随机推荐

  1. 《Android开发从零开始》——25.数据存储
  2. 2014.01.21 ——— android 关联android-
  3. 使用NetBeans搭建Android开发环境
  4. android 使用html5作布局文件: webview跟
  5. Android系统配置数据库注释(settings.db)
  6. 锁屏界面
  7. Android(安卓)环境搭建
  8. android“设置”里的版本号
  9. IM-A820L限制GSM,WCDMA上网的原理(其他泛泰
  10. Android(安卓)拨号器的简单实现