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:orientation="vertical"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/eatId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭"
/>
<CheckBox
android:id="@+id/sleepId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉"
/>
<CheckBox
android:id="@+id/playId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打豆豆"
/>
<CheckBox
android:id="@+id/allId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/>
<CheckBox
android:id="@+id/oppositeId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选"
/>
<CheckBox
android:id="@+id/allnoId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全不选"
/>
</LinearLayout>
MainActivity类:
package com.mcm.checkbox;
import android.os.Bundle;
import android.app.Activity;
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.ListView;
public class MainActivity extends Activity {
private CheckBox eatBox;
private CheckBox sleepBox;
private CheckBox playBox;
private CheckBox allBox;
private CheckBox oppositeBox;
private CheckBox allnoBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eatBox= (CheckBox) findViewById(R.id.eatId);
sleepBox= (CheckBox) findViewById(R.id.sleepId);
playBox= (CheckBox) findViewById(R.id.playId);
allBox = (CheckBox) findViewById(R.id.allId);
oppositeBox = (CheckBox) findViewById(R.id.oppositeId);
allnoBox = (CheckBox) findViewById(R.id.allnoId);
/*
CheckBoxListener listener = new CheckBoxListener();
eatBox.setOnClickListener(listener);
sleepBox.setOnClickListener(listener);
playBox.setOnClickListener(listener);
*/
CheckBoxListener listener = new CheckBoxListener();
eatBox.setOnCheckedChangeListener(listener);
sleepBox.setOnCheckedChangeListener(listener);
playBox.setOnCheckedChangeListener(listener);
allBox.setOnCheckedChangeListener(listener);
oppositeBox.setOnCheckedChangeListener(listener);
allnoBox.setOnCheckedChangeListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class CheckBoxListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(buttonView.getId() == R.id.eatId){
System.out.println("eatBox");
}else if(buttonView.getId() == R.id.sleepId){
System.out.println("sleepBox");
}else if(buttonView.getId() == R.id.playId){
System.out.println("playBox");
}else if(buttonView.getId() == R.id.allId){//全选
System.out.println("allBox");
if(isChecked == true){
eatBox.setChecked(true);
sleepBox.setChecked(true);
playBox.setChecked(true);
}
oppositeBox.setChecked(false);
allnoBox.setChecked(false);
}else if(buttonView.getId() == R.id.oppositeId){//反选
System.out.println("oppositeBox");
if(eatBox.isChecked() == true){
eatBox.setChecked(false);
}else{
eatBox.setChecked(true);
}
if(sleepBox.isChecked() == true){
sleepBox.setChecked(false);
}else{
sleepBox.setChecked(true);
}
if(playBox.isChecked() == true){
playBox.setChecked(false);
}else{
playBox.setChecked(true);
}
allBox.setChecked(false);
allnoBox.setChecked(false);
}else if(buttonView.getId() == R.id.allnoId){//全不选
System.out.println("allnoBox");
if(isChecked == true){
eatBox.setChecked(false);
sleepBox.setChecked(false);
playBox.setChecked(false);
allBox.setChecked(false);
oppositeBox.setChecked(false);
}
}
/*
if(isChecked == true){
System.out.println("checked");
}else{
System.out.println("unchecked");
}
*/
}
}
//CheckBox的使用方法
/*
class CheckBoxListener implements OnClickListener{
@Override
public void onClick(View v) {
if(v.getId() == R.id.eatId){
System.out.println("eatBox");
}else if(v.getId() == R.id.sleepId){
System.out.println("sleepBox");
}else if(v.getId() == R.id.playId){
System.out.println("playBox");
}
CheckBox box = (CheckBox) v;
if(box.isChecked()){
System.out.println("checked");
}else{
System.out.println("unchecked");
}
}
}
*/
}