Android沉浸模式下弹出键盘后收起键盘无法隐藏Navigation Bar问题

问题表现:全屏沉浸模式下弹出Dialog,键盘输入完成后解散Dialog并收起键盘,但Navigation Bar又出来了

在一些机型上这个问题很容易重现,尝试在Activity的onWindowFocusChanged方法中重新setSystemUiVisibility

private void hideSystemUI() {
  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_LOW_PROFILE
      | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

然而键盘收起时Navigation Bar还是有几率不收起来,后来找到了这,发现需要延迟一点处理。下面是完整代码:

/**
 * @param hasFocus 活动是否获得焦点
 */
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    Cog.d(TAG, "onWindowFocusChanged hasFocus=", hasFocus);
    // When the window loses focus (e.g. the action overflow is shown),
    // cancel any pending hide action. When the window gains focus,
    // hide the system UI.
    if (hasFocus) {
        mHandler.removeCallbacks(mHideSystemUi);
        mHandler.postDelayed(mHideSystemUi, 300);
    } else {
        mHandler.removeCallbacks(mHideSystemUi);
    }
}

private void hideSystemUI() {
    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_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

private Runnable mHideSystemUi = new Runnable() {
    @Override
    public void run() {
        hideSystemUI();
    }
};

附送重新显示Navigation Bar方法:

private void showSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

你可能感兴趣的:(Android沉浸模式下弹出键盘后收起键盘无法隐藏Navigation Bar问题)