Android CheckBox控件使用OnClickListener和OnCheckedChangeListener两种不同的方法监听

Android中CheckBox控件使用可以通过两种方法实现
1、OnClickListener
2、OnCheckedChangeListener

布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<CheckBox
android:id="@+id/all_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="全选" />

<CheckBox
android:id="@+id/first_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="选项1" />

<CheckBox
android:id="@+id/secend_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="选项2" />

<CheckBox
android:id="@+id/third_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="选项3" />

<CheckBox
android:id="@+id/fourth_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="选项4" />

</LinearLayout>


控制文件
package com.geoffrey.practice_checkbox;

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

public class MainActivity extends Activity {
private CheckBox allCheckBox;
private CheckBox firstCheckBox;
private CheckBox secendCheckBox;
private CheckBox thirdCheckBox;
private CheckBox fourthCheckBox;

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

allCheckBox = (CheckBox) findViewById(R.id.all_checkbox);
firstCheckBox = (CheckBox) findViewById(R.id.first_checkbox);
secendCheckBox = (CheckBox) findViewById(R.id.secend_checkbox);
thirdCheckBox = (CheckBox) findViewById(R.id.third_checkbox);
fourthCheckBox = (CheckBox) findViewById(R.id.fourth_checkbox);

// 给第一个和第二个CheckBox控件绑定OnCheckBoxClickListener方法
OnCheckBoxClickListener listener = new OnCheckBoxClickListener();
firstCheckBox.setOnClickListener(listener);
secendCheckBox.setOnClickListener(listener);

// 给第三个和第四个CheckBox控件绑定CheckBoxListener方法
CheckBoxListener listener2 = new CheckBoxListener();
thirdCheckBox.setOnCheckedChangeListener(listener2);
fourthCheckBox.setOnCheckedChangeListener(listener2);

// 全选,全部取消
allCheckBox.setOnClickListener(listener);

}

// 内部类OnCheckBoxClickListener,使用OnClickListener方法监听
class OnCheckBoxClickListener implements OnClickListener {

@Override
public void onClick(View view) {
CheckBox checkBox = (CheckBox) view;
if (checkBox.getId() == R.id.first_checkbox) {
System.out.println("点击第一个checkbox");
} else if (checkBox.getId() == R.id.secend_checkbox) {
System.out.println("点击第二个checkbox");
}

if (checkBox.isChecked()) {
System.out.println("选中");
if (checkBox.getId() == R.id.all_checkbox) {
firstCheckBox.setChecked(true);
secendCheckBox.setChecked(true);
thirdCheckBox.setChecked(true);
fourthCheckBox.setChecked(true);
}
} else {
System.out.println("取消选中");
allCheckBox.setChecked(false);
if (checkBox.getId() == R.id.all_checkbox) {
firstCheckBox.setChecked(false);
secendCheckBox.setChecked(false);
thirdCheckBox.setChecked(false);
fourthCheckBox.setChecked(false);
}
}

}

}

// 内部类CheckBoxListener,使用OnCheckedChangeListener方法监听
class CheckBoxListener implements OnCheckedChangeListener {

@Override
/**
* @param buttonView 点击选中的控件
* @param isChecked 是否选中状态
*/
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView.getId() == R.id.third_checkbox) {
System.out.println("点击第三个checkbox");
} else if (buttonView.getId() == R.id.fourth_checkbox) {
System.out.println("点击第四个checkbox");
}

if (isChecked) {
System.out.println("选中");
} else {
System.out.println("取消选中");
allCheckBox.setChecked(false);
}

}

}

}



[img]http://dl2.iteye.com/upload/attachment/0097/6100/40dbf912-9347-31b2-9efe-7897426dac47.png[/img]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值