java 如何在Android中再次设置布尔值true?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33614806/
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 21:55:07  来源:igfitidea点击:

How to set a boolean true again in Android?

javaandroidbooleanonclicklistener

提问by Alex Mamo

The boolean isCheckedis declared first time true, than i set in the OnClickListenerto false. What i neeed, is to set boolean isCheckedagain to truein the next OnClickListener. I know that are not in the same scope but how can i do that? This is my code:

boolean isChecked是第一次声明true,而不是我在OnClickListenerto 中设置的false。我需要的是在下boolean isChecked一次再次设置。我知道这不在同一范围内,但我该怎么做?这是我的代码:trueOnClickListener

answer[j].setOnClickListener(new View.OnClickListener() {
    private boolean isChecked = true;

    @Override
    public void onClick(View v) {
        RadioButton checkedRadioButton = ((RadioButton) v);
        if (isChecked) {
        if (checkedRadioButton.isChecked()) {
            score++;
            isChecked = false;
        }
        }
    }
});

resetButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        isChecked = true;
        score = 0;
    }
});

Thanks!

谢谢!

回答by basic

You just need to declare your boolean outside of the onclick listeners scope. It will then become more accessbile and you can do whatever you need with it. Declare it before your onCreate so it has global access in this class.

您只需要在 onclick 侦听器范围之外声明您的布尔值。然后它会变得更易于访问,你可以用它做任何你需要的事情。在 onCreate 之前声明它,以便它在此类中具有全局访问权限。

Example

例子

public class MainActivity extends AppCompatActivity {
Button btn, btn2;
boolean isValid;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);
    btn2 = (Button) findViewById(R.id.button2);
    isValid = true;

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isValid = true;
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isValid = false;
        }
    });

  }
}

EDIT V2

编辑 V2

I took a look at the code you posted again and from what I can tell your logic is saying if isValid is true (you have if(isValid)) then go into your logic. Well to start you are actually declaring the value as false. So in your code say to being isValid = true; What is happening is your logic is saying the boolean isn't true so don't execute the code.

我查看了您再次发布的代码,我可以告诉您的逻辑是说如果 isValid 为真(您有 if(isValid)),然后进入您的逻辑。首先,您实际上是将值声明为 false。所以在你的代码中说 isValid = true; 发生的事情是你的逻辑说布尔值不是真的所以不要执行代码。

Edit V3

编辑 V3

Alright I took a look again at all of your code and I can see what is happening. Since the variable is global now it isn't executing a second time once we declare it as false because it never gets a chance to be reassigned to true again. To be honest I don't see the purpose of this variable. It isn't doing anything other than just being there haha. However if you want to have the variable you should just remove the line isChecked = false; As I said I honestly see no purpose for it in your code.

好吧,我再次查看了您的所有代码,我可以看到发生了什么。由于该变量现在是全局变量,因此一旦我们将其声明为 false,它就不会第二次执行,因为它再也没有机会被重新分配为 true。老实说,我没有看到这个变量的目的。它除了在那里什么都不做,哈哈。但是,如果您想拥有该变量,则只需删除该行 isChecked = false; 正如我所说,老实说,我在您的代码中看不到它的用途。

回答by Arun Ganessh

Make It SIMPLE-------

让它变得简单-------

btn = (Button) findViewById(R.id.button);
    btn2 = (Button) findViewById(R.id.button2);
    isValid = true;

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isValid = true;
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isValid = false;
        }