| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2033 人关注过本帖
标题:为checkBox设置事件监听,来判断有几个checkBox被选中
只看楼主 加入收藏
a1024204390
Rank: 2
来 自:河南周口郸城
等 级:论坛游民
帖 子:87
专家分:67
注 册:2012-6-27
结帖率:88.89%
收藏
 问题点数:0 回复次数:0 
为checkBox设置事件监听,来判断有几个checkBox被选中
为七个checkbox设置事件监听,确保只能有三个checkbox被选中。下面是我写的代码,但是没达到效果,还请指点。
Main3.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.import 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>

搜索更多相关主题的帖子: checkBox android app private text 
2018-06-16 17:03
快速回复:为checkBox设置事件监听,来判断有几个checkBox被选中
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027008 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved