我将沉浸式模式添加到我的应用程序中。这是代码:

 @Override
 public void onWindowFocusChanged(boolean hasFocus) {
     super.onWindowFocusChanged(hasFocus);
     if (hasFocus)
     {
         getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                 | View.SYSTEM_UI_FLAG_FULLSCREEN
                 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
     }
 }

但是,如果我在键盘上打字然后关闭(使用后退按钮,通过在屏幕上单击),导航栏仍保持显示状态,则需要缩小/重新打开该应用程序才能返回沉浸式模式。

关闭键盘后如何返回沉浸式模式?

编辑:这是一个Cordova应用程序

最佳答案

我使用处理程序来检测用户的不 Activity 状态,然后隐藏系统ui。它会自动检测用户是否未在屏幕上进行交互,然后在5秒钟后自动隐藏系统用户界面

//Declare handler
private var timeoutHandler: Handler? = null
private var interactionTimeoutRunnable: Runnable? = null

在onCreate()中
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
. . .

       //Initialise handler
        timeoutHandler =  Handler();
        interactionTimeoutRunnable =  Runnable {
            // Handle Timeout stuffs here
            hideSystemUI()
        }

        //start countdown
        startHandler()

. . .

处理焦点变化的方法
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) hideSystemUI()
    }

皮革系统Ui
    private fun hideSystemUI() {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }


    // Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
    private fun showSystemUI() {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    }



// reset handler on user interaction
override fun onUserInteraction() {
    super.onUserInteraction()
    resetHandler()
}

//restart countdown
fun resetHandler() {
    timeoutHandler!!.removeCallbacks(interactionTimeoutRunnable);
    timeoutHandler!!.postDelayed(interactionTimeoutRunnable, 5*1000); //for 10 second

}

// start countdown
fun startHandler() {
    timeoutHandler!!.postDelayed(interactionTimeoutRunnable, 5*1000); //for 10 second
}

关于android - 在Android上关闭键盘后返回沉浸式模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23456144/

10-11 00:42