Android控件之Checkbox和RadioButton

Checkbox和RadioButton监听事件都是:

OnCheckedChangeListener//当选中的改变监听

参考代码:

package com.example.checkboxandradiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener {
	private CheckBox music,game,vidio;
	private RadioButton famale,male;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();
		
	}
	//初始化UI控件
	public void	initUI(){
		//CheckBox控件
		music=(CheckBox) findViewById(R.id.music);
		game=(CheckBox) findViewById(R.id.game);
		vidio=(CheckBox) findViewById(R.id.vidio);
		
		music.setOnCheckedChangeListener(this);
		game.setOnCheckedChangeListener(this);
		vidio.setOnCheckedChangeListener(this);
		
		//RadioButton控件
		famale=(RadioButton) findViewById(R.id.famale);
		male=(RadioButton) findViewById(R.id.male);
		
		famale.setOnCheckedChangeListener(this);
		male.setOnCheckedChangeListener(this);
		
		
	}
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch (buttonView.getId()) {
		case R.id.music:
			Toast.makeText(getApplicationContext(), music.getText()+"/"+isChecked, 1000).show();
			break;
		case R.id.game:
			Toast.makeText(getApplicationContext(), game.getText()+"/"+isChecked, 1000).show();
			break;
		case R.id.vidio:
			Toast.makeText(getApplicationContext(), vidio.getText()+"/"+isChecked, 1000).show();
			break;
		case R.id.famale:
			Toast.makeText(getApplicationContext(), famale.getText()+"/"+isChecked, 1000).show();
			break;
		case R.id.male:
			Toast.makeText(getApplicationContext(), male.getText()+"/"+isChecked, 1000).show();
			break;
		}
	}

	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王家视频教程图书馆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值