简体   繁体   English

不断“检查” Android片段可见性/键盘可见性的最佳方法

[英]Best way to constantly “check” fragment visibility/keyboard visibility in android

I would like to continuously check if a fragment is visible or better yet. 我想不断检查片段是否可见或更好。 If the keyboard is present in an Android application. 如果键盘存在于Android应用程序中。 What would be the best method to go about this? 最好的方法是什么?

I'm not too familiar with the activity lifecycle, but I'm hoping this is correct way. 我对活动生命周期不太熟悉,但是我希望这是正确的方法。 The code below is written in the OnCreate method. 下面的代码是用OnCreate方法编写的。 I need it to run constantly though. 我需要它不断运行。

if (timeFragment.isVisible()){
    Toast.makeText(getApplicationContext(), "Time visible", Toast.LENGTH_LONG).show();
}

For a fragment, the function setUserVisibleHint(boolean isVisible) is fired everytime the fragment becomes visible or invisible. 对于片段,每次片段可见或不可见时都会触发setUserVisibleHint(boolean isVisible)函数。

This will conserve resources compared to polling. 与轮询相比,这将节省资源。 In timeFragment: 在timeFragment中:

public void setUserVisibleHint(boolean isVisible) {
    if (isVisible)
        Toast.makeText(getActivity(), "Time becoming visible", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(getActivity(), "Time becoming invisible", Toast.LENGTH_LONG).show();
}

Hope this helps... 希望这可以帮助...

如果您真正想要的是检查软键盘当前是否在屏幕上(正如我从上面对我的评论的答复中所假定的那样),则可以使用这种颇为不客气的侦听器方法。

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

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