1.什么是ChexkBox?
复选框允许用户在同一个组中选择一个或对个选项。
2.ChexBox的使用
CheckBox的监听事件:setOnChekedChangeListener(),当checkbox选中的状态改变的时触发的监听。
需求:选中checkbox时提示选中的文本信息,点击确定按钮时显示最终的选中文本信息,实现全选功能
(1)先绑定checkbox的状态监听事件,选中某一项时给出提示
(2)然后绑定按钮的点击事件,点击时判断最终选中的checkbox,将选中的文本给出提示。
(3)实现全选功能
全选
如果所有的checkbox全被选中则 全选被选中
如果全选被选中则所有的checkbox均被选中
如果取消全选则所有的checkbox都取消
代码:
.xml文件中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="请选择你的爱好:" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="敲代码" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="唱歌" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="click"
/>
<CheckBox
android:id="@+id/all_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:onClick="click"
/>
</LinearLayout>
activity文件中的代码
public class MainActivity extends ActionBarActivity {
// 声明控件p
rivate CheckBox cb1, cb2, cb3;
private MyOnChekedChangeListener listener;
private CheckBox all_cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 关联空件
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
// 设置监听(以内部类的形式设置监听事件)
listener = new MyOnChekedChangeListener();
cb1.setOnCheckedChangeListener(listener);
cb2.setOnCheckedChangeListener(listener);
cb3.setOnCheckedChangeListener(listener);
all_cb=(CheckBox) findViewById(R.id.all_cb);
//点击确定按钮监听事件
public void click(View view){
switch(view.getId()){
case R.id.btn1:
String str=check();
Toast.makeText(MainActivity.this,"选中的是"+str, Toast .LENGTH_SHORT).show();
break;
//当点击 了全选按钮,则checkbox全部被选中
case R.id .all_cb:
boolean bl=all_cb.isChecked();
cb1.setChecked(bl);
cb2.setChecked(bl);
cb3.setChecked(bl);
break;
}
//定义一个方法,获取checkbox最终选中的文本
public String check(){
//设置一个字符串缓冲区,你选中的是什么就可以存到缓冲区里面
StringBuilder sb=new StringBuilder();
if(cb1.isChecked()){
sb.append(cb1.getText().toString());//表示追加,将选中的内容添加到缓冲区
}
if(cb2.isChecked()){
sb.append(cb2.getText().toString());//表示追加,将选中的内容添加到缓冲区
}
if(cb3.isChecked()){
sb.append(cb3.getText().toString());//表示追加,将选中的内容添加到缓冲区
}
return sb.toString();//返回值是缓冲区的内容
}
// 内部类形式设置监听事件
class MyOnChekedChangeListener implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// CompoundButton buttonView:表示当前状态切换的控件对象,其返回值是一个buttonView,所以要强转
// boolean isChecked 表示当前控件的选中状态
//获取选中状态的文本并且给出提示
// if(isChecked){
// CheckBox cb = (CheckBox) buttonView;
// String str = cb.getText().toString();// 获取选中的内容
// Toast.makeText(MainActivity.this, “选中的是” + str, Toast.LENGTH_SHORT)
// .show();
// }
//如果发现列表中的checkbox全部被选中则全选被选中,否则全选不被选中
if(cb1.isChecked()&&cb2.isChecked()&&cb3.isChecked()){
all_cb.setChecked(true);
}else{
all_cb.setChecked(false);
}
}
}
}