1

I have a View with some GPS info. It is made VISIBLE/GONE with a GPS on/off switch inside the app. I want to call setKeepScreenOn(true) on this view so that it keeps screen on but ONLY when it is VISIBLE and NOT when it is GONE.

4

1 回答 1

1

我会说它也适用于视图消失的情况,但我不知道。不过测试应该不难。

但是您可以View.setKeepScreenOn(false)在将其可见性设置为GONE以及View.setKeepScreenOn(true)将其可见性设置为 时使用VISIBLE

View view = ...; // I guess something with findViewById() happens here
view.setVisibility(View.GONE);
view.setKeepScreenOn(false);
...
view.setVisibility(View.VISIBLE);
view.setKeepScreenOn(true);

或者您不必记住这一点,您可以为此定义一个方法:

private void mySetVisibility(View v, int visibility) {
    v.setVisibility(visibility);
    if (visibility == View.GONE) {
        v.setKeepScreenOn(false);
    } else {
        v.setKeepScreenOn(true);
    }
}

然后只需使用mySetVisibility(view, View.GONE)and mySetVisibility(view, View.VISIBLE)

很简单。

希望能帮助到你。

于 2013-04-29T14:00:33.133 回答