注册 登录
编程论坛 Android开发

为checkBox设置事件监听,来判断有几个checkBox被选中

a1024204390 发布于 2018-06-16 17:07, 2918 次点击
为七个checkbox设置事件监听,确保只能有三个checkbox被选中。下面是我写的代码,但是没达到效果,还请指点。
main.java
程序代码:
package com.example.lly.denglu;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android. android.widget.RadioButton;
import android.widget.Toast;
import java.lang.String;


public class Main3Activity extends AppCompatActivity {

    private RadioButton nan=null;
    private RadioButton nv=null;
    private Button save=null;
    private CheckBox ai1=null;
    private CheckBox ai2=null;
    private CheckBox ai3=null;
    private CheckBox ai4=null;
    private CheckBox ai5=null;
    private CheckBox ai6=null;
    private CheckBox ai7=null;

    private int i1=0;
    private int i2=0;
    private int i3=0;
    private int i4=0;
    private int i5=0;
    private int i6=0;
    private int i7=0;
    private int ii=0;


    private String aihao0 = " ";
    private String aihao1 = " ";
    private String aihao2 = " ";
    private String aihao3 = " ";
    private String aihao4 = " ";
    private String aihao5 = " ";
    private String aihao6 = " ";
    private String xingbie = " ";



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        nan = (RadioButton) super.findViewById(R.id.radioButton1);
        nv = (RadioButton) super.findViewById(R.id.radioButton2);
        ai1 = (CheckBox) super.findViewById(R.id.checkBox1);
        ai2 = (CheckBox) super.findViewById(R.id.checkBox2);
        ai3 = (CheckBox) super.findViewById(R.id.checkBox3);
        ai4 = (CheckBox) super.findViewById(R.id.checkBox4);
        ai5 = (CheckBox) super.findViewById(R.id.checkBox5);
        ai6 = (CheckBox) super.findViewById(R.id.checkBox6);
        ai7 = (CheckBox) super.findViewById(R.id.checkBox7);
        save = (Button) super.findViewById(R.id.button3);
        save.setOnClickListener(new SaveOnClickListener());

        ai1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {

                    aihao0 = ai1.getText().toString();i1=1;
                }
                else{
                    i1=0;
                    aihao0 ="";
                }
            }
        });

        ai2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    aihao1 = ai2.getText().toString();i2=1;

                }
                else{
                    i2=0;
                    aihao0 ="";
                }
            }
        });
        ai3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    aihao2 = ai3.getText().toString();i3=1;
                }
                else{
                    i3=0;
                    aihao0 ="";
                }
            }
        });
        ai4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    aihao3 = ai4.getText().toString();i4=1;
                }
                else{
                    i4=0;
                    aihao0 ="";
                }
            }
        });
        ai5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    aihao4 = ai5.getText().toString();i5=1;

                }
                else{
                    i5=0;
                    aihao0 ="";
                }
            }
        });

        ai6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    aihao5 = ai6.getText().toString();i6=1;

                }
                else{
                    i6=0;
                    aihao0 ="";
                }
            }
        });
        ai7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {

                    aihao6 = ai7.getText().toString();i7=1;
                }
                else{
                        i7=0;
                    aihao0 ="";
                }
            }
        });
        ii=i1+i2+i3+i4+i5+i6+i7;
    }
    private class SaveOnClickListener implements View.OnClickListener {
        public void onClick(View v) {
            if (nan.isChecked())
                xingbie = nan.getText().toString();
            if (nv.isChecked())
                xingbie = nv.getText().toString();
            if(ii != 3)
                Toast.makeText(getApplicationContext(), "7选3哦!", Toast.LENGTH_LONG).show();
            else {
                String sInfo = "性别:" + xingbie + "*****爱好:" + aihao0 + aihao1 + aihao2 + aihao3 + aihao4 + aihao5 + aihao6;
                Toast.makeText(getApplicationContext(), sInfo, Toast.LENGTH_LONG).show();
            }

        }

    }
}


main3.xml
程序代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas. xmlns:app="http://schemas. xmlns:tools="http://schemas. android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lly.denglu.Main3Activity"
    tools:layout_editor_absoluteY="81dp">

    <Button
        android:id="@+id/button3"
        android:layout_width="88dp"
        android:layout_height="49dp"
        android:layout_marginBottom="159dp"
        android:text="保存"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="49dp"
        android:text="篮球"
        app:layout_constraintBaseline_toBaselineOf="@+id/checkBox2"
        app:layout_constraintStart_toEndOf="@+id/checkBox2" />

    <CheckBox
        android:id="@+id/checkBox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="43dp"
        android:text="足球"
        app:layout_constraintStart_toStartOf="@+id/checkBox2"
        app:layout_constraintTop_toBottomOf="@+id/checkBox2" />

    <CheckBox
        android:id="@+id/checkBox7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="网球"
        app:layout_constraintBaseline_toBaselineOf="@+id/checkBox6"
        app:layout_constraintEnd_toEndOf="parent" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="7dp"
        android:text="游泳"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="健身"
        app:layout_constraintBaseline_toBaselineOf="@+id/checkBox5"
        app:layout_constraintStart_toStartOf="parent" />

    <CheckBox
        android:id="@+id/checkBox6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="43dp"
        android:text="书法"
        app:layout_constraintStart_toStartOf="@+id/checkBox3"
        app:layout_constraintTop_toBottomOf="@+id/checkBox3" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="7dp"
        android:text="象棋"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="80dp"
        android:layout_height="64dp"
        android:layout_marginBottom="30dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="36dp"
        app:layout_constraintBottom_toTopOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="女" />

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="65dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="男" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="43dp"
        android:layout_marginEnd="3dp"
        android:layout_marginTop="130dp"
        android:text="7选3哦!"
        app:layout_constraintEnd_toEndOf="@+id/radioGroup"
        app:layout_constraintStart_toStartOf="@+id/checkBox1"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>
1 回复
#2
xiajue2018-10-13 23:48
这样写太繁琐,且耦合高,代码太多不易维护。你可以给他们设置onClickListener(this);
然后switch判断点击的Id和cb.isChecked()来写你的逻辑。
基本就是:
    如果:id==你要的checkbox  并且 状态为选中{
        要取消选中的checkbox.setChecked(false);
        //你的更多逻辑
    }否则 如果:id==你要的checkbox  并且 状态为选中{
        要取消选中的checkbox.setChecked(false);
        //你的更多逻辑
    }.....以此类推



或者你可以把你的操作封装成一个方法
private List<CheckBox> mCheckBoxs;//记录当前的checkBox状态,在onCreate的时候初始化它,并把所有checkBox添加进去
private static final int MAX_CHECK_SIZE=3;//最大的可选数量
//id为要选中的id
private void setCheckBoxCheck(int id){
    int checkSize;//当前选中的数量
    CheckBox tempCb;//保存一个临时cb
    for(CheckBox cb:mCheckBoxs){
        if(cb.isChecked()){
            tempCb=cb;
            checkSize++;
        }
        
        if(cb.getId()==id){
            cb.setChecked(true);
            if(checkSize<MAX_CHECK_SIZE){
                tempCb.setChecked(false);//将上一个选中的cb取消选中
            }
       }  
    }
}


然后还是在onClick的时候,直接调用这一个方法应该就行了

代码是直接撸的,没有在ide里测试。如果有错误也是有可能滴,不过逻辑就是这个逻辑。用一个list保存所有checkBox,每次点击的时候先把当前点击的cb选中,然后判断当前已经选中多少个,超过限制大小的话就取消选中一个,以此类推。就能控制最大选中了。
1