import android.os.Bundle;
import android.app.Activity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity {
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); swimBox = (CheckBox) findViewById(R.id.swim);
runBox = (CheckBox) findViewById(R.id.run);
readBox = (CheckBox) findViewById(R.id.read); CheckBoxListener listener = new CheckBoxListener();
swimBox.setOnCheckedChangeListener(listener);
runBox.setOnCheckedChangeListener(listener);
readBox.setOnCheckedChangeListener(listener);
/*
//多个控件可以使用同一个监听器
OnBoxClickListener listener = new OnBoxClickListener();
swimBox.setOnClickListener(listener);
runBox.setOnClickListener(listener);
readBox.setOnClickListener(listener);*/
} //
class CheckBoxListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(buttonView.getId()==R.id.read){
System.out.println("read");
}else if(buttonView.getId()==R.id.run){
System.out.println("run");
}else if(buttonView.getId()==R.id.swim){
System.out.println("swim");
}
if(isChecked){
System.out.println("Checked");
}else{
System.out.println("UnChecked");
}
} } /* OnClickListener的使用方法
class OnBoxClickListener implements OnClickListener{
public void onClick(View view) { //CheckBox是View的子类,所以可以接收
CheckBox box = (CheckBox)view;
if(view.getId()==R.id.read)
System.out.println("read");
else if(view.getId()==R.id.run)
System.out.println("run");
else if(view.getId()==R.id.swim)
System.out.println("swim"); if(box.isChecked()){//isChecked方法不是view中的方法所以要向下转型,该方法可以判段是否被选中,如果选中返回真
System.out.println("Checked");
}else
System.out.println("unChecked");
} }
*/
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打游戏"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉"
/>
</LinearLayout>
实现全选功能:
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity {
private CheckBox all;
private CheckBox swim;
private CheckBox run;
private CheckBox read;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); all = (CheckBox) findViewById(R.id.all);
swim = (CheckBox) findViewById(R.id.swim);
run = (CheckBox) findViewById(R.id.run);
read = (CheckBox) findViewById(R.id.read); AllCheckListener listener = new AllCheckListener();
all.setOnCheckedChangeListener(listener);
}
class AllCheckListener implements OnCheckedChangeListener{
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { swim.setChecked(isChecked);
run.setChecked(isChecked);
read.setChecked(isChecked);
/*
if(isChecked){
swim.setChecked(true);
run.setChecked(true);
read.setChecked(true);
}else{
swim.setClickable(false);
run.setChecked(false);
read.setChecked(false);
}
*/
} } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <CheckBox
android:id="@+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/> <CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打游戏"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉"
/>
</LinearLayout>