打开键盘时隐藏底部导航栏

时间:2020-02-07 18:57:11

标签: java android android-softkeyboard bottomnavigationview

我制作了一个带有如下聊天功能的应用程序:

enter image description here

现在,当我单击EditText时,我希望它可以隐藏BottomNavigationBar,看起来像这样:

enter image description here

但是,当我关闭键盘时,我希望它再次向我显示BottomNavigationBar。但事实并非如此。

我正在使用以下代码进行操作:

sView = findViewById(R.id.Sv_Chats);
sView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        heightDiff = sView.getRootView().getHeight() - sView.getHeight();
        Log.d( "HEIGHT", Integer.toString( heightDiff ));
    }
});

final EditText editText = findViewById(R.id.et_Message);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!isVisibleWhileSoftKeyboardShowing(editText) && hasFocus) {
            sView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    sView.smoothScrollBy(0, 200);
                    bottomNavigationView.setVisibility(View.GONE);
                }
            }, 200);
        }
    }
});

我尝试使用:

    final EditText editText = findViewById(R.id.et_Message);
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!isVisibleWhileSoftKeyboardShowing(editText) && hasFocus) {
                sView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        sView.smoothScrollBy(0, 200);
                        //bottomNavigationView.setVisibility(View.GONE);
                    }
                }, 200);
            } else {bottomNavigationView.setVisibility(View.VISIBLE)}
        }
    });

但是,它没有用。

此外,我设置了一个日志以向我显示heightDiff的值,但是由于某种原因,它总是被调用两次,因此我认为它使底部可见,但将其返回到隐藏状态。

有什么方法可以正确隐藏和显示它吗?

谢谢

0 个答案:

没有答案