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

单选按钮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;
	
	@Override
	protected 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{
		@Override
		public 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{
		@Override
		public void onCheckedChanged(CompoundButton button, boolean isChecked){
			String sFav="";
			if(isChecked){
				sFav=cbFootBall.getText().toString();
				sFav=sFav+"选中!";
			}
			else
				sFav=sFav+"未迁中";
			Toast.makeText(getApplicationContext(), "您选择的爱好是:"+sFav, Toast.LENGTH_LONG).show();
		}
	}
	
	private class BasketBallOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{
		@Override
		public void onCheckedChanged(CompoundButton button,boolean isChecked){
			String sFav="";
			if(cbBasketBall.isChecked()){
				sFav=cbBasketBall.getText().toString();
				sFav=sFav+"选中!";
			}
			else
				sFav=sFav+"未迁中";
			Toast.makeText(getApplicationContext(), "您选择的爱好是:"+sFav, Toast.LENGTH_LONG).show();
		}
	}
	
}

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

  效果如下:

  

  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒋会全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值