Android隐藏导航栏/保持沉浸式模式与软键盘外观

前端之家收集整理的这篇文章主要介绍了Android隐藏导航栏/保持沉浸式模式与软键盘外观前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用使用沉浸式模式的客户端应用程序,使用以下代码隐藏每个活动的导航栏和状态栏:
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中也不会显示导航栏.任何人都可以想到总是使用导航按钮(背面/隐藏键盘,家庭等)的方法,即使在使用键盘时总是隐藏?

解决方法

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

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

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;
                }
              }
            });
原文链接:https://www.f2er.com/android/313504.html

猜你在找的Android相关文章