请教:setChecked(false)没生效

漂白了的四壁 2018-06-14 04:42:17
在七种爱好中保持只能选择三项爱好,checkBox前的勾只能有三个被勾选,想用setChecked(false)但是一直没成功,初学者请教



package com.example.lly.denglu;

/**
* Created by lly on 2018/6/14.
*/

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;


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;

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());

}

private class SaveOnClickListener implements View.OnClickListener{
public void onClick(View v){
String xingbie=" ";
String aihao0=" ";
String aihao1=" ";
String aihao2=" ";
String aihao3=" ";
String aihao4=" ";
String aihao5=" ";
String aihao6=" ";

if(nan.isChecked())
xingbie=nan.getText().toString();
if(nv.isChecked())
xingbie=nv.getText().toString();

int i=0;
if (ai1.isChecked() && i<3 ){i++;
aihao0 = ai1.getText().toString();}
if(i>=4)ai1.setChecked(false);
if (ai2.isChecked() && i<3){i++;
aihao1 = ai2.getText().toString();}
if(i>=4)ai2.setChecked(false);
if (ai3.isChecked()&&i<3){i++;
aihao2 = ai3.getText().toString();}
if(i>=4)ai3.setChecked(false);
if (ai4.isChecked()&&i<3){i++;
aihao3 = ai4.getText().toString();}
if(i>=4)ai4.setChecked(false);
if (ai5.isChecked()&&i<3){i++;
aihao4 = ai5.getText().toString();}
if(i>=4)ai5.setChecked(false);
if (ai6.isChecked()&&i<3){i++;
aihao5 = ai6.getText().toString();}
if(i>=4)ai6.setChecked(false);
if (ai7.isChecked()&&i<3){i++;
aihao6 = ai7.getText().toString();}
if(i>=4)ai7.setChecked(false);

String sInfo="性别:"+xingbie+"*****爱好:"+aihao0+aihao1+aihao2+aihao3+aihao4+aihao5+aihao6;
Toast.makeText(getApplicationContext(),sInfo,Toast.LENGTH_LONG).show();

}
}

}
...全文
3048 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
千寻港生惠香 2018-06-25
  • 打赏
  • 举报
回复
应该可以吧。这种较为简单的需求做下测试就行了~
漂白了的四壁 2018-06-16
  • 打赏
  • 举报
回复
引用 6 楼 wldrzx 的回复:
public void onCheckedChanged(CompoundButton var1, boolean var2){ if(var2){ flag++; }else{ flag--; } ifListener(); } public void ifListener(){ Log.i("if:",flag+""); if(flag >= 3){ if(!c1.isChecked()){ c1.setEnabled(false); } if(!c2.isChecked()){ c2.setEnabled(false); } if(!c3.isChecked()){ c3.setEnabled(false); } if(!c4.isChecked()){ c4.setEnabled(false); } if(!c5.isChecked()){ c5.setEnabled(false); } if(!c6.isChecked()){ c6.setEnabled(false); } if(!c7.isChecked()){ c7.setEnabled(false); } }else { c1.setEnabled(true); c2.setEnabled(true); c3.setEnabled(true); c4.setEnabled(true); c5.setEnabled(true); c6.setEnabled(true); c7.setEnabled(true); } }
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.widget.CompoundButton;
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();
            }

        }

    }
}
这样行吗?
漂白了的四壁 2018-06-16
  • 打赏
  • 举报
回复
引用 4 楼 laosanlly 的回复:
[quote=引用 3 楼 cxz003 的回复:] setOnCheckedChangeListener里面勾上加一 去掉减一 满3个了把未勾选的setenable(false)
但是我不知道该怎么实现它,刚接触[/quote]
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.widget.CompoundButton;
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();
            }

        }

    }
}
这样嘛?
漂白了的四壁 2018-06-16
  • 打赏
  • 举报
回复
引用 7 楼 maogedadada 的回复:
xml文件贴出来
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 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="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="156dp" android:text="保存" app:layout_constraintBottom_toBottomOf="parent" tools:layout_editor_absoluteX="161dp" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球" app:layout_constraintBaseline_toBaselineOf="@+id/checkBox2" app:layout_constraintStart_toStartOf="@+id/checkBox6" /> <CheckBox android:id="@+id/checkBox5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="109dp" android:layout_marginStart="109dp" android:text="足球" app:layout_constraintEnd_toEndOf="@+id/checkBox6" app:layout_constraintStart_toStartOf="@+id/checkBox4" tools:layout_editor_absoluteY="284dp" /> <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_marginBottom="43dp" android:text="游泳" app:layout_constraintBottom_toTopOf="@+id/checkBox5" app:layout_constraintStart_toStartOf="@+id/checkBox5" /> <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_marginEnd="41dp" android:text="书法" app:layout_constraintEnd_toStartOf="@+id/checkBox7" tools:layout_editor_absoluteY="284dp" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:text="象棋" app:layout_constraintBaseline_toBaselineOf="@+id/checkBox2" app:layout_constraintStart_toStartOf="parent" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="110dp" android:layout_height="93dp" android:layout_marginStart="16dp" android:layout_marginTop="36dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="男" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="女" /> </RadioGroup> </android.support.constraint.ConstraintLayout>
maogedadada 2018-06-15
  • 打赏
  • 举报
回复
xml文件贴出来
千寻港生惠香 2018-06-15
  • 打赏
  • 举报
回复
public void onCheckedChanged(CompoundButton var1, boolean var2){ if(var2){ flag++; }else{ flag--; } ifListener(); } public void ifListener(){ Log.i("if:",flag+""); if(flag >= 3){ if(!c1.isChecked()){ c1.setEnabled(false); } if(!c2.isChecked()){ c2.setEnabled(false); } if(!c3.isChecked()){ c3.setEnabled(false); } if(!c4.isChecked()){ c4.setEnabled(false); } if(!c5.isChecked()){ c5.setEnabled(false); } if(!c6.isChecked()){ c6.setEnabled(false); } if(!c7.isChecked()){ c7.setEnabled(false); } }else { c1.setEnabled(true); c2.setEnabled(true); c3.setEnabled(true); c4.setEnabled(true); c5.setEnabled(true); c6.setEnabled(true); c7.setEnabled(true); } }
qq_42197511 2018-06-15
  • 打赏
  • 举报
回复
好阿 哈哈 看看
漂白了的四壁 2018-06-14
  • 打赏
  • 举报
回复
引用 3 楼 cxz003 的回复:
setOnCheckedChangeListener里面勾上加一 去掉减一 满3个了把未勾选的setenable(false)
但是我不知道该怎么实现它,刚接触
  • 打赏
  • 举报
回复
setOnCheckedChangeListener里面勾上加一 去掉减一 满3个了把未勾选的setenable(false)
寒冰大神 2018-06-14
  • 打赏
  • 举报
回复
应该在checkbox的点击事件 或 setOnCheckedChangeListener里控制
千寻港生惠香 2018-06-14
  • 打赏
  • 举报
回复
应该在checkbox的点击事件里面控制吧

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧