使用键盘时如何隐藏底部导航视图?

时间:2018-09-19 17:30:59

标签: c# android android-layout xamarin.android android-bottom-nav-view

在主要任务中,对于我的应用程序,我从用户那里获取了大量输入。当前,当他们进入输入时,键盘会升起,并且底部导航视图在其上方保持可见。但是我想发生的是,当键盘弹出时,底部导航栏应该消失并且对用户不可见。所以我要这样做吗?

MainActivity的代码,其中包含底部导航栏。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/maintoolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_below="@id/maintoolbar"
        android:layout_above="@+id/bottom_navigation"/>

    <android.support.design.widget.BottomNavigationView     
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        app:elevation="4dp"
        app:menu="@menu/bottom_navigation_main"/>

</RelativeLayout>

我希望转换尽可能快,整洁,以便用户几乎不会注意到更改。

2 个答案:

答案 0 :(得分:0)

在您的AndroidManifest.xml文件的MainActivity的活动标签中添加此标签:

android:windowSoftInputMode="adjustPan"

编辑:如果上述解决方案不起作用,则解决方法: 在每个编辑文本上添加Focus更改侦听器。如果获得焦点,键盘将显示出来。因此,隐藏您的底视图。

View.OnFocusChangeListener myFocusListener = new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            bottomNavigationView.setVisiblity(View.GONE);
        } else {

            bottomNavigationView.setVisiblity(View.VISIBLE);

        }
    }
};

editText1.setOnFocusChangeListener(myFocusListener);
editText2.setOnFocusChangeListener(myFocusListener);
...

答案 1 :(得分:0)

您可以尝试在“活动”顶部添加WindowSoftInputMode=SoftInput.AdjustPan,如下所示:

[Activity(WindowSoftInputMode=SoftInput.AdjustPan)]

另一个选择是将ViewSourceObserver添加到键盘,并在出现键盘时隐藏BottomNavigationView,然后在键盘消失时显示BottomNavigationView。不过第一种选择可能会更好。