A02_CheckBox的两种监听器设置

目标:1.使用OnCheckedChangeListener和OnclickListener两种监听器为CheckBox添加监听器。

2.CheckBox用不同颜色区分不同的监听器。

3.增加全选操作。

4.使用Toast显示选择状态

效果一,模拟器环境Android4.2显示:



效果二,真机Android2.3.7显示:


代码:

activity_main.xml

<LinearLayout 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"
    android:background="@drawable/tree"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <CheckBox
        android:id="@+id/eatBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃饭饭"
        android:textColor="#0ff000" />

    <CheckBox
        android:id="@+id/sleepBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡觉觉"
        android:textColor="#0ff000" />

    <CheckBox
        android:id="@+id/beatBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打豆豆"
        android:textColor="#0ff000" />

    <CheckBox
        android:id="@+id/allCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全选"
        android:textColor="#0ff000" />

    <CheckBox
        android:id="@+id/sisterBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="妹妹"
        android:textColor="#0fffff" />

    <CheckBox
        android:id="@+id/brotherBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哥哥"
        android:textColor="#0fffff" />

</LinearLayout>

MainActivity.java

package com.haut.a02_checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
	// 声明CheckBox
	private CheckBox eatBox;
	private CheckBox sleepBox;
	private CheckBox beatBox;
	private CheckBox allCheckBox;
	private CheckBox sisterBox;
	private CheckBox brotherBox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 获取CheckBox
		eatBox = (CheckBox) findViewById(R.id.eatBox);
		sleepBox = (CheckBox) findViewById(R.id.sleepBox);
		beatBox = (CheckBox) findViewById(R.id.beatBox);
		allCheckBox = (CheckBox) findViewById(R.id.allCheckBox);
		sisterBox = (CheckBox) findViewById(R.id.sisterBox);
		brotherBox = (CheckBox) findViewById(R.id.brotherBox);

		// 使用OnclickListener为CheckBox添加监听器
		BoxOnClickListener boxOnClickListener = new BoxOnClickListener();
		eatBox.setOnClickListener(boxOnClickListener);
		sleepBox.setOnClickListener(boxOnClickListener);
		beatBox.setOnClickListener(boxOnClickListener);
		allCheckBox.setOnClickListener(boxOnClickListener);
		
		// 使用OnCheckedChangeListener为CheckBox添加监听器
		BoxOnCheckedChangeListener boxOnCheckedChangeListener = new BoxOnCheckedChangeListener();
		sisterBox.setOnCheckedChangeListener(boxOnCheckedChangeListener);
		brotherBox.setOnCheckedChangeListener(boxOnCheckedChangeListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	// 使用OnclickListener为CheckBox添加监听器
	class BoxOnClickListener implements OnClickListener {

		public void onClick(View v) {
			CheckBox checkBox = (CheckBox) v;
			if (checkBox.getId() == R.id.eatBox) {
				if (checkBox.isChecked()) {
					Toast.makeText(MainActivity.this, "已选吃饭饭",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, "吃饭饭取消已选",
							Toast.LENGTH_SHORT).show();
				}
			} else if (checkBox.getId() == R.id.sleepBox) {
				if (checkBox.isChecked()) {
					Toast.makeText(MainActivity.this, "已选睡觉觉",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, "睡觉觉取消已选",
							Toast.LENGTH_SHORT).show();
				}
			} else if (checkBox.getId() == R.id.beatBox) {
				if (checkBox.isChecked()) {
					Toast.makeText(MainActivity.this, "已选打豆豆",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, "打豆豆取消已选",
							Toast.LENGTH_SHORT).show();
				}
			}else if(checkBox.getId() == R.id.allCheckBox){
				if (checkBox.isChecked()) {
					eatBox.setChecked(true);
					sleepBox.setChecked(true);
					beatBox.setChecked(true);
				} else {
					eatBox.setChecked(false);
					sleepBox.setChecked(false);
					beatBox.setChecked(false);
				}

			}
		}

	}

	// 使用OnCheckedChangeListener为CheckBox添加监听器
	class BoxOnCheckedChangeListener implements OnCheckedChangeListener {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if (buttonView.getId() == R.id.sisterBox) {
				if (isChecked) {
					Toast.makeText(MainActivity.this, "已选妹妹",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, "妹妹取消已选",
							Toast.LENGTH_SHORT).show();
				}
			} else if (buttonView.getId() == R.id.brotherBox) {
				if (isChecked) {
					Toast.makeText(MainActivity.this, "已选哥哥",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(MainActivity.this, "哥哥取消已选",
							Toast.LENGTH_SHORT).show();
				}
			}
		}

	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值