Android - 可见和不可见的按钮

时间:2012-08-06 13:13:19

标签: java android

如果数组中有一个或多个对象,我试图让一个按钮变为可见,否则它将保持不可见。

我用过:

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");
}

我遇到的问题是它运行并且在启动时我使用的数组是空的,因此按钮保持不可见,但是当我向数组添加对象时它仍然保持不可见,表明它不运行代码试。

我已将上面的代码放在onCreate中,有人能告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:3)

当您更改对象数组时,您需要运行onCreate中的代码 - 您现在只有在启动时检查的内容,并且根本没有链接到要更改的数组。 / p>

所以如果你有伪代码:

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
相关问题