Android 隐藏导航栏/使用软键盘外观保持沉浸式模式 [英] Android Hide Navigation Bar/Stay in Immersive Mode with Soft Keyboard Appearance

查看:81
本文介绍了Android 隐藏导航栏/使用软键盘外观保持沉浸式模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用沉浸式模式使用以下代码隐藏每个活动的导航栏和状态栏的客户端应用程序:

Working on a client's app that is using immersive mode to hide the navigation bar and status bar on every activity using the following code:

int currentApiVersion = android.os.Build.VERSION.SDK_INT;

final int flags = 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;

// This work only for android 4.4+
if (currentApiVersion >= 19) {

        getWindow().getDecorView().setSystemUiVisibility(flags);
        // Code below is for case when you press Volume up or Volume down.
        // Without this after pressing valume buttons navigation bar will
        // show up and don't hide
        final View decorView = getWindow().getDecorView();
        decorView
                .setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {

                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            decorView.setSystemUiVisibility(flags);
                        }
                }
         });
}

唯一的问题是他们希望应用程序保持沉浸式模式,即使软键盘显示可以输入 EditText 也不显示导航栏.任何人都可以想出一种方法,即使在使用键盘时也始终隐藏导航按钮(后退/隐藏键盘、主页等)?

The only problem is that they would like the app to stay in immersive mode and not show the navigation bar even when the soft keyboard is showing to type into an EditText. Can anyone think of a way to always have the navigation buttons (back/hide keyboard, home, etc) always be hidden even while using the keyboard?

推荐答案

我想出了一个解决方法,可以检查每个内部导航栏的状态,尝试隐藏它并再次(一次又一次)检查它.

I've come up with a workaround that checks the navigation bar's status for every internal, try to hide it and check it again (and again).

>

这是一段代码,确保在软键盘关闭后 2 秒内隐藏导航栏.

Here's some piece of code, that makes sure the navigation bar is hidden in 2 seconds after the soft keyboard is closed.

  private final Runnable checkSystemUiRunnable = new Runnable() {
    @Override
    public void run() {
      checkHideSystemUI();
    }
  };

  private void checkHideSystemUI() {
    // Check if system UI is shown and hide it by post a delayed handler
    if (isSystemUiShown) {
      hideSystemUI();
      handler.postDelayed(checkSystemUiRunnable, SYSTEM_UI_HIDE_DELAY);
    }
  }

  private void hideSystemUI() {
    decorView.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 // hide nav bar
                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE);
  }

  // In onCreate()
    decorView.setOnSystemUiVisibilityChangeListener(
            new View.OnSystemUiVisibilityChangeListener() {
              @Override
              public void onSystemUiVisibilityChange(int visibility) {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                  handler.postDelayed(checkSystemUiRunnable, SYSTEM_UI_HIDE_DELAY);
                  isSystemUiShown = true;
                } else {
                  isSystemUiShown = false;
                }
              }
            });

这篇关于Android 隐藏导航栏/使用软键盘外观保持沉浸式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆