CheckBox 复选框,多选按钮
/*
* CheckBox 复选框,多选按钮
*
可以提供给用户在多个选项之间实现多选效果
* 一个CheckBox代表多选中的一个选项
*/
package com.example.kn_day04_3_checkbox;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
*
实现功能:全选、反选、全不选、获取选中项功能
*
* CheckBox 复选框 多选按钮
* 一个CheckBox代表多选中的一个选项
* 可以提供给用户在多个选项之间 实现多选效果
*
* 注:OnCheckedChangeListener
* 位于android.widget.CompoundButton.OnCheckedChangeListener
*
* @author KNOWN
*/
public class CheckBoxActivity extends Activity
implements OnClickListener,
OnCheckedChangeListener
{
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
//用于存储当前用户的所有选项
ArrayList<String> strList = new ArrayList<String>();
//存放CheckBox列表 将所有CheckBox放在一个集合中,方便操作
ArrayList<CheckBox> cbList = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cbList.add(cb1);
cbList.add(cb2);
cbList.add(cb3);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
// 设置单击事件
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
// 设置选中状态改变事件
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}
@Override
public void onClick(
View v) {
// TODO Auto-generated method stub
switch (
v.getId()) {
// 获取当前相中项
case
R.id.btn1:
Log.i("当前选中了:", strList.toString());
break;
// 全选
case
R.id.btn2:
for (int i = 0; i < cbList.size(); i++) {
cbList.get(i).setChecked(true);
}
break;
//全不选
case
R.id.btn3:
for (int i = 0; i < cbList.size(); i++) {
cbList.get(i).setChecked(false);
}
break;
//反选
case
R.id.btn4:
for (int i = 0; i < cbList.size(); i++) {
//方法1:if判断
/*if(cbList.get(i).isChecked()){
cbList.get(i).setChecked(false);
}else{
cbList.get(i).setChecked(true);
}*/
//方法2:将当前状态改变为相反的状态
cbList.get(i).toggle();
}
break;
default:
break;
}
}
/**
*
监听CheckBox状态改变事件
* 每当CheckBox从选中变成未选中,或者从未选中变为选中均会触发此方法
*
buttonView
代表 当前CheckBox
*
isChecked代表当前CheckBox是否选中状态
*/
@Override
public void
onCheckedChanged(CompoundButton
buttonView, boolean
isChecked) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) buttonView;
if (isChecked) {//添加到选中项里
strList.add(cb.getText().toString());
Log.i("新选中了:", cb.getText().toString());
} else {//从选中项里移除
strList.remove(cb.getText().toString());
Log.i("取消选中:", cb.getText().toString());
}
}
}
*******
activity_check_box.xml
<RelativeLayout 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: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="com.example.kn_day04_3_checkbox.CheckBoxActivity" >
<TextView android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textSize="28sp"
android:text="请选择爱好:" />
<CheckBox android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text="唱歌"
/>
<CheckBox android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb1"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text="跳舞"
android:checked="true"
/>
<CheckBox android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb2"
android:layout_alignLeft="@+id/cb2"
android:layout_alignStart="@+id/cb2"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text="运动"
/>
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb3"
android:layout_alignLeft="@+id/cb3"
android:layout_alignStart="@+id/cb3"
android:layout_marginTop="20dp"
android:layout_marginLeft="-20dp"
android:textSize="28sp"
android:text=" 获取选中项"
android:focusable="true"
/>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn1"
android:layout_alignLeft="@+id/btn1"
android:layout_alignStart="@+id/btn1"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text=" 全选"
/>
<Button android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn2"
android:layout_alignLeft="@+id/btn2"
android:layout_alignStart="@+id/btn2"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text=" 全不选"
/>
<Button android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn3"
android:layout_alignLeft="@+id/btn3"
android:layout_alignStart="@+id/btn3"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text=" 反选"
/>
</RelativeLayout>
xmlns:tools=" http://schemas.android.com/tools"
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="com.example.kn_day04_3_checkbox.CheckBoxActivity" >
<TextView android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:textSize="28sp"
android:text="请选择爱好:" />
<CheckBox android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text="唱歌"
/>
<CheckBox android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb1"
android:layout_marginLeft="60dp"
android:layout_marginStart="60dp"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text="跳舞"
android:checked="true"
/>
<CheckBox android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb2"
android:layout_alignLeft="@+id/cb2"
android:layout_alignStart="@+id/cb2"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text="运动"
/>
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb3"
android:layout_alignLeft="@+id/cb3"
android:layout_alignStart="@+id/cb3"
android:layout_marginTop="20dp"
android:layout_marginLeft="-20dp"
android:textSize="28sp"
android:text=" 获取选中项"
android:focusable="true"
/>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn1"
android:layout_alignLeft="@+id/btn1"
android:layout_alignStart="@+id/btn1"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text=" 全选"
/>
<Button android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn2"
android:layout_alignLeft="@+id/btn2"
android:layout_alignStart="@+id/btn2"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text=" 全不选"
/>
<Button android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn3"
android:layout_alignLeft="@+id/btn3"
android:layout_alignStart="@+id/btn3"
android:layout_marginTop="20dp"
android:textSize="28sp"
android:text=" 反选"
/>
</RelativeLayout>