简体   繁体   English

打开和关闭键盘后,导航栏再次显示

[英]Navigation Bar gets visible again after open and close keyboard

private int currentApiVersion; private int currentApiVersion;

private void hideNavigationBar() {
    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 >= Build.VERSION_CODES.KITKAT) {

        getWindow().getDecorView().setSystemUiVisibility(flags);

        // Code below is to handle presses of Volume up or Volume down.
        // Without this, after pressing volume buttons, the navigation bar will
        // show up and won'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);
                        }
                    }
                });
    }

}


@SuppressLint("NewApi")
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (currentApiVersion >= Build.VERSION_CODES.KITKAT && 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);
    }
}

i called hideNavigationBar method from onCreate, but its not work for me. 我从onCreate调用hideNavigationBar方法,但是对我来说不起作用。 when i open keybard on clicking EditText and then close keyboard, but navigation bar not get invisible. 当我单击EditText打开键盘并关闭键盘时,导航栏不可见。

You have to hide navigation bar when closing the keyboard. 关闭键盘时,您必须隐藏导航栏。 Refer below code for this. 请参考下面的代码。 It will help you 会帮你的

final View activityRootView = ((ViewGroup) getActivity().findViewById(android.R.id.content)).getChildAt(0);

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        private final int DefaultKeyboardDP = 100;
        private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
        private final Rect r = new Rect();
        private boolean wasOpened;
        @Override
        public void onGlobalLayout() {
            // Convert the dp to pixels.
            int estimatedKeyboardHeight = (int) TypedValue
                    .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());
            // Conclude whether the keyboard is shown or not.
            activityRootView.getWindowVisibleDisplayFrame(r);
            int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
            boolean isShown = heightDiff >= estimatedKeyboardHeight;
            if (isShown == wasOpened) {
                Log.d("Keyboard state", "Ignoring global layout change...");
                return;
            }
            wasOpened = isShown;
            if (isShown) {
                // Show keyborad
            } else {
                // Hide keyboard 
            }
        }
    });

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

相关问题 如何使导航栏仅在软键盘打开时可见? - How can I make the navigation bar visible only when the soft keyboard is open? 键盘关闭后Android操作栏消失 - Android action bar disappearing after keyboard close 关闭键盘后导航栏不会隐藏 - Navigation bar wont hide after closing keyboard 用户手动向上滚动导航栏后如何再次隐藏导航栏? - How to hide navigation bar again after user scrolled it manually up? 使用打开的键盘无法看到EditText - EditText Is Not Visible With Open Keyboard Android edittext在关闭并打开键盘后移开焦点 - Android edittext remove focus after close and open keyboard 键盘打开/关闭事件后如何更改视图 - How to make view change after keyboard open/close event 在工具栏中的 SearchView 打开的情况下更新 RecyclerView 视图后关闭软键盘? - Close soft keyboard after RecyclerView view is updated with SearchView in Toolbar open? 如何使用 java 检测 Android 底部导航栏上的关闭键盘按钮 - How can I detect the close keyboard button at the bottom navigation bar in Android using java 显示对话框后或 EditText 获得焦点时,隐藏的导航栏(带有主页、后退和概览按钮的底部栏)可见 - Hidden Navigation bar (bottom bar with Home, Back and Overview button) is visible after showing a dialog or if the EditText gains focus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM