A02_CheckBox的两种监听器设置

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

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

          3.增加全选操作。

          4.使用Toast显示选择状态

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


A02_CheckBox的两种监听器设置_第1张图片

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

A02_CheckBox的两种监听器设置_第2张图片

代码:

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();
				}
			}
		}

	}
}


你可能感兴趣的:(编程,android,checkbox,手机)