自定义CheckBox快速实现开发

在开发CheckBox过程中,经常感觉功能很简单,就是写了半天代码,尤其是设置监听和判断上,万一有10个以上的CheckBox,还会在复制时由于不小心,出现bug 天啦,这么简单常用的控件出BUG,简直没脸呆了

下面自己写了一个demo,感觉不会出现上面情况了,代码也少了很多,再多CheckBox也不会多增代码

 

CheckBoxActivity.java
package com.mw.app.view.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mw.app.R;
import com.mw.app.common.vo.Const;
import com.mw.app.view.element.CheckBoxView;

import java.util.LinkedList;

public class CheckBoxActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Const.CheckBoxViewList = new LinkedList<CheckBoxView>();
        setContentView(R.layout.checkbox_layout);
    }

    public  void getCheckBoxValue(View view){
        StringBuffer sbText = new StringBuffer();
        StringBuffer vText = new StringBuffer();
        for (CheckBoxView m : Const.CheckBoxViewList) {
            if(m.isChecked()){
                sbText.append(m.getText());
                sbText.append(",");
                vText.append(m.getTag());
                vText.append(",");
            }
        }
        Toast.makeText(getApplicationContext(),"内容:"+sbText.toString()+"值:"+vText.toString(),Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Const.CheckBoxViewList.clear();
        Const.CheckBoxViewList = null;
        Const.all_select_Btn = null;
    }
}

 

CheckBoxView.java
package com.mw.app.view.element;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.CompoundButton;

import com.mw.app.common.vo.Const;

public class CheckBoxView extends androidx.appcompat.widget.AppCompatCheckBox implements CompoundButton.OnCheckedChangeListener {
    private final String ALL_SELECT = "ALL_SELECT";

    public CheckBoxView(Context context) {
        super(context);
    }

    public CheckBoxView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnCheckedChangeListener(this);

        if(ALL_SELECT.equals(String.valueOf(this.getTag()))){
            Const.all_select_Btn = this;
        }else{
            Const.CheckBoxViewList.add(this);
        }
    }

    public CheckBoxView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Object tag = buttonView.getTag();
        //如果是全选按扭
       if (tag!= null && ALL_SELECT.equals(String.valueOf(tag)) ) {
           buttonView.setText(isChecked?"全弃选":("全选"));
           if (!buttonView.isPressed()) { //判断view是否是用户点击
               return;
           }
            for (CheckBoxView m : Const.CheckBoxViewList) {
                m.setChecked(isChecked);
            }

        }else if(!isChecked){//如果是取消选中
           Const.all_select_Btn.setChecked(isChecked);
       }else{//如果是选中
           boolean isAllSelected = true;
           for (CheckBoxView m : Const.CheckBoxViewList) {
               if(!m.isChecked()){
                   isAllSelected = false;
                   break;
               }
           }
           if(isAllSelected){
               Const.all_select_Btn.setChecked(true);
           }
       }
    }

}
Const.java 
package com.mw.app.common.vo;

import com.mw.app.view.element.CheckBoxView;

import java.util.LinkedList;

public class Const {

    public static LinkedList<CheckBoxView> CheckBoxViewList;
    public static CheckBoxView all_select_Btn;
}

 

checkbox_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.mw.app.view.element.CheckBoxView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="全选"
        android:tag="ALL_SELECT"
        android:id="@+id/checkbox_layout_allSelect"></com.mw.app.view.element.CheckBoxView>


    <com.mw.app.view.element.CheckBoxView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="checkbox1"
        android:tag="1"
        android:id="@+id/checkbox1"></com.mw.app.view.element.CheckBoxView>

    <com.mw.app.view.element.CheckBoxView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="checkbox2"
        android:tag="2"
        android:id="@+id/checkbox2"></com.mw.app.view.element.CheckBoxView>

    <com.mw.app.view.element.CheckBoxView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="checkbox3"
        android:tag="3"
        android:id="@+id/checkbox3"></com.mw.app.view.element.CheckBoxView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getCheckBoxValue"
        android:text="获取"></Button>
</LinearLayout>

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页