简体   繁体   English

Android-可见和不可见按钮

[英]Android - visible and invisible buttons

I am trying to get a button to become visible if there is one or more objects in an array, otherwise it will stay invisible. 我试图让一个按钮在数组中有一个或多个对象时变得可见,否则它将保持不可见。

I am used: 我习惯了:

if (positionOverlay.geoPointsArrayList.size() <= 0){
    buttonClear.setVisibility(View.GONE);
    System.out.println("Clear button hidden");
}
else if (positionOverlay.geoPointsArrayList.size() >= 1) {
    buttonClear.setVisibility(View.VISIBLE);
    System.out.println("Clear button visible");
}

The problem I am having is that it runs and on start up the array i am using is empty, so the button stays invisible, however when I add objects to the array it still stays invisible, suggesting that it doesnt run the code again. 我遇到的问题是它可以运行,并且在启动时正在使用的数组为空,因此按钮保持不可见,但是当我将对象添加到数组时,它仍然保持不可见,这表明它不会再次运行代码。

I have placed the code above in the onCreate, could anyone tell me where i have gone wrong? 我已将上面的代码放在onCreate中,有人可以告诉我我哪里出错了吗?

When you alter your array of objects you'll need to run the code you have in onCreate - what you have now only ever checks on startup and isn't linked to the array being altered at all. 当您更改对象数组时,您将需要运行onCreate的代码-您现在所拥有的仅在启动时检查,并且根本不链接到要更改的数组。

So in pseudocode if you have: 所以用伪代码,如果你有:

array.add("new item");
this.updateButton(); // This is where you have your button code.

array.remove("some other item");
this.updateButton(); // Check if you've gone below the limit again

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM