java 复选框中的 OnclickListener (Android Studio)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34797307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:22:11  来源:igfitidea点击:

OnclickListener in a checkbox (Android Studio)

javaandroidcheckboxonclicklistener

提问by liav bahar

here is part of my code which includes the CheckBox(ChckBoxNo):

这是我的代码的一部分,其中包括 CheckBox(ChckBoxNo):

    final CheckBox ChckBoxNo = (CheckBox)promptsView.findViewById(R.id.ChkBoxNo);

                                  ChckBoxNo.setChecked(true);

                                   ChckBoxNo.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           if (ChckBoxNo.isChecked()) {

                                         ChckBoxNo.setChecked(false);
                                           }
                                           else if (!ChckBoxNo.isChecked())
                                           {
                                               ChckBoxNo.setChecked(true);
                                           }
                                       }
                                   });

At the begining I set true for the isChecked() method on my checkbox , then I implement the onclicklistener on the checkbox. When I run the app the checkbox is Checked as I defined eralier , but when I click on the checkbox ,It's unchecked and then immidately checked again(I didnt clciked again on the checkbox! ) What should I do in order to fix that,what wrong in my code ? Thanks!

开始时,我将复选框上的 isChecked() 方法设置为 true,然后在复选框上实现 onclicklistener。当我运行应用程序时,复选框被选中,因为我定义了eralier,但是当我点击复选框时,它被取消选中,然后立即再次选中(我没有再次点击复选框!)我该怎么做才能解决这个问题,什么我的代码有问题吗?谢谢!

回答by lase

A Checkboxwill handle the "checking" process automatically - you do not have to manage this yourself for the standard usage.

ACheckbox将自动处理“检查”过程 - 对于标准用法,您不必自己管理。

Check out this examplefrom the docs. Here, when a click event is caught, they are doing operations based on the isChecked()state.

从文档中查看此示例。在这里,当点击事件被捕获时,他们正在根据isChecked()状态进行操作。

回答by Lucas

try this:

试试这个:

checkBox = (CheckBox) findViewById(R.id.checkBox);
            checkBox.setChecked(checkPasswordExist());
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (checkBox.isChecked()) {
                        // your code to checked checkbox                        
                        }
                    } else {
                        // your code to  no checked checkbox
                    }
                }
            });

回答by Josef Korbel

You're battling the CheckBox. It's standart behavior that checkbox change states, you dont need to do that by yourself.

您正在与 CheckBox 作斗争。复选框更改状态是标准行为,您无需自己执行此操作。

Remove setChecked true and false and paste something usefull there instead )

删除 setChecked true 和 false 并在那里粘贴一些有用的东西)

回答by Andreas Schmidt

You dont need to set the checked state on the same that you have klicked.

您不需要将选中的状态设置为与您点击的相同。

You shold handle some other Operations on specific state.

您应该处理特定状态的其他一些操作。