检查Activity是否存在视图

时间:2014-10-02 17:08:42

标签: android

我有一个辅助方法,它传入当前活动并将其强制转换为正确的活动实例。我在布局中有一个可选的xml按钮字段,可能是GONE,具体取决于不同的情况。如何从我对活动的引用中检查按钮是否可见?代码如下。

 private boolean ShowDialogIfButtonExists(BaseActivity screen)
 {
      //return true if button from activity is visible
      return false;
 }

3 个答案:

答案 0 :(得分:2)

你的问题不清楚。您是否需要检查布局中是否存在View,或者它是否可见?

在第一种情况下,您可以简单地使用方法findViewById:如果结果为null,则视图不在布局中。在第二种情况下,找到视图并检查它是否可见:

view.getVisibility() == View.VISIBLE

答案 1 :(得分:1)

你的回报应该是这样的:

return screen.findViewById(R.id.button).getVisibility() == VISIBLE;

答案 2 :(得分:1)

使用此方法:

private boolean ShowDialogIfButtonExists(BaseActivity screen) {
     return screen.findViewById(R.id.button).getVisibility() == View.VISIBLE;
 }