简体   繁体   English

在软键盘出现时出现状态栏但在沉浸模式下软键盘消失时不会隐藏

[英]Status Bar appears when soft keyboard appears but not hidden back when soft keyboard disappears while in Immersive Mode

Initially I set my Activity to be in Immersive Mode with the following code: 最初我使用以下代码将我的Activity设置为沉浸模式:

View decorView = getWindow().getDecorView();
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
    | View.SYSTEM_UI_FLAG_FULLSCREEN
    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

Everything works fine until the user clicks on an EditText and the soft keyboard appears. 一切正常,直到用户点击EditText并出现软键盘。 It causes the Status Bar to stick on the top and never hides back again even after the soft keyboard disappears. 它会导致状态栏粘在顶部,即使在软键盘消失后也不会再次隐藏。 Strangely enough, I only encounter this problem on LG/Samsung phones, Sony phones do not have any problem with this. 奇怪的是,我只在LG /三星手机上遇到过这个问题,索尼手机对此没有任何问题。 Anyone has any idea about this? 有人对此有任何想法吗?

Use immersive mode like this. 像这样使用沉浸式模式。

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        immersiveMode();
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    immersiveMode();
                }
            });
    }

Here is your immersive mode function. 这是您的沉浸式模式功能。

public void immersiveMode() {
        final View decorView = getWindow().getDecorView();
        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
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
              }

Also call immersiveMode() in OnResume() ; 也可以在OnResume()中调用immersiveMode () ; Now status bar will disappear as your soft key board disappears. 现在,当软键板消失时,状态栏将消失。

Here is my solution for this ; 这是我的解决方案; First I checked if soft keyboard is showed up or not: 首先,我检查了软键盘是否显示:

getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Rect r = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
            int screenHeight = getWindow().getDecorView().getRootView().getHeight();

            int keypadHeight = screenHeight - r.bottom;

            //Log.d(TAG, "keypadHeight = " + keypadHeight);

            if (keypadHeight > screenHeight * 0.15) { 
                 //Keyboard is opened
                 hideNavBar();
            }
            else {
                // keyboard is closed
            }
        }
    });

And I have a hideNavBar() method to be triggered when soft keyboard is showed up. 我有一个hideNavBar()方法在软键盘出现时被触发。

private void hideNavBar() {
if (Build.VERSION.SDK_INT >= 19) {
    View v = getWindow().getDecorView();
    v.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);
}

} }

This solves the problem of getting navigation bar while there is an Edittext to be typed. 这解决了在输入要编辑的Edittext时获取导航栏的问题。

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

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