Android学习之CheckBox(附加全选功能)

前言

​ 最近在写Android作业,其中一个就是复选框CheckBox的使用,在此记录一下写的过程,本人是只萌新,好多地方写的还有些不规范或是写的不对欢迎大佬们提点。

​ 先看一下做出来的效果

在这里插入图片描述

==设计思路:==当点击全选时所有的选择框都选择上,再点击时取消选择,当下面的子选项选择时判断是否全部选中或是全部取消选中,根据获取的状态改变全选CheckBox的选中状态,当点击提交时将选中的文本用Toast显示出来。

1、布局

​ 布局文件就是写了几个比较简单的基础组件。文字都在string.xml文件里定义,这里直接使用。

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_title"
        android:textColor="#000"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000" />

    <CheckBox
        android:id="@+id/all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_all"
        android:layout_marginLeft="10dp" />

    <CheckBox
        android:id="@+id/apple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_apple"
        android:layout_marginLeft="20dp" />

    <CheckBox
        android:id="@+id/banana"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_banana"
        android:layout_marginLeft="20dp" />

    <CheckBox
        android:id="@+id/watermelon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_watermelon"
        android:layout_marginLeft="20dp" />

    <CheckBox
        android:id="@+id/peach"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_peach"
        android:layout_marginLeft="20dp" />

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_commit"
        android:layout_marginLeft="10dp"
        android:background="#BFEFFF" />


</LinearLayout>

2、定义变量并获取组件id

String remind="";  //用来记录提交按钮点击后Toast显示的文字
private CheckBox all,apple,banana,watermelon,peach;  //各个CheckBox
private Button submit;  //提交按钮

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  all = (CheckBox) findViewById(R.id.all);
  apple = (CheckBox) findViewById(R.id.apple);
  banana = (CheckBox) findViewById(R.id.banana);
  watermelon = (CheckBox) findViewById(R.id.watermelon);
  peach = (CheckBox) findViewById(R.id.peach);
  submit = (Button) findViewById(R.id.submit);
 }

3、监听全选按钮

//一下方法监听全选按钮的选中状态的改变,选中时子选项都选中,取消选中子选项也全部取消
all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

	@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if (isChecked) {
      	apple.setChecked(true);   //设置CheckBox的选中状态
        banana.setChecked(true);
        watermelon.setChecked(true);
        peach.setChecked(true);
       }else {
        apple.setChecked(false);
        banana.setChecked(false);
        watermelon.setChecked(false);
        peach.setChecked(false);
       }
     }
 });

4、子选项的监听

​ 因为子选项过多,不可能去一个一个的写监听函数,所以在这里定义了一个通用的监听。

private CompoundButton.OnCheckedChangeListener cb=new CompoundButton.OnCheckedChangeListener() { //实例化一个cb
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                if (apple.isChecked()&&banana.isChecked()&&watermelon.isChecked()&&peach.isChecked()) {
                    all.setChecked(true);
                }else {
                    all.setChecked(false);
                }
            }else {
                boolean a1 =apple.isChecked();
                boolean a2 =banana.isChecked();
                boolean a3 =watermelon.isChecked();
                boolean a4 =peach.isChecked();
                all.setChecked(false);
                apple.setChecked(a1);
                banana.setChecked(a2);
                watermelon.setChecked(a3);
                peach.setChecked(a4);
            }
        }
    };

然后在onCreate()里面为各个checkbox添加状态监听器

 apple.setOnCheckedChangeListener(cb);
 banana.setOnCheckedChangeListener(cb);
 watermelon.setOnCheckedChangeListener(cb);
 peach.setOnCheckedChangeListener(cb);

5、提交监听

submit.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    remind = "喜欢的水果:";
    if (apple.isChecked()) {
    	remind+=apple.getText();
    }
    if (banana.isChecked()) {
        remind+=banana.getText();
    }
    if (watermelon.isChecked()) {
        remind+=watermelon.getText();
    }
    if (peach.isChecked()) {
        remind+=peach.getText();
    }
    Toast toast = Toast.makeText(MainActivity.this,remind,Toast.LENGTH_LONG);
    toast.show();
  }
});

6、总结

​ 在写的代码里有几个问题:

​ ①定义的CheckBox变量过多,如果子选项过多这样子定义肯定是不行的,觉得可以使用数组的方式定义,同时再定义一个数据用来记录他们的选中状态。

​ ②在子选项选择时当选择全部后在取消一个选项时我写的是先把他们所有状态记录下来然后把全选给取消选中,然后再把其他的选中,这样子选择时会闪动一下,目前没有想到解决方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值