当键盘出现时隐藏“底部导航栏” - Android

57

我有一个小型的聊天UI演示应用程序。该应用程序具有底部导航栏。当键盘出现时,我需要隐藏底部导航栏。

这是聊天UI的示例

如您所见,当您点击EditText元素时,键盘会出现,但底部导航栏仍可见。我已尝试使用此度量方法等方法,但UI元素会闪烁

是否有一种适当的方法可以在键盘可见时隐藏底部导航栏?

编辑: 在以下活动中,您可以看到我设置键盘侦听器以在确定为可见键盘时调整UI元素的位置。

这是我的活动代码,在onCreateView中使用上面链接中的setKeyboardListener方法设置:

package uk.cal.codename.projectnedry.TeamChatFragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.roughike.bottombar.BottomBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import uk.cal.codename.projectnedry.R;
import uk.cal.codename.projectnedry.TeamChatFragment.ListAdapter.TeamChatListAdapter;
import uk.demo.cal.genericmodelviewpresenter.GenericMvp.GenericMvpFragment;

import static android.view.View.GONE;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link TeamChatView.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link TeamChatView#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TeamChatView extends GenericMvpFragment implements TeamChatContract.RequiredViewOps {

    private OnFragmentInteractionListener mListener;
    @BindView(R.id.teamChatList)
    RecyclerView mTeamChatRecyclerView;
    @BindView(R.id.teamChatSendButton)
    ImageButton mTeamChatSendButton;
    @BindView(R.id.messageTextInput)
    EditText mMessageTextInput;
    TeamChatListAdapter mTeamChatListAdapter;
    TeamChatListAdapter.ClickListener mTeamChatListClickListener;
    private ArrayList<String> mTestMessageList;

    public interface OnKeyboardVisibilityListener {
        void onVisibilityChanged(boolean visible);
    }

    public final void setKeyboardListener(final OnKeyboardVisibilityListener listener) {
        final View activityRootView = ((ViewGroup) getActivity().findViewById(android.R.id.content)).getChildAt(0);

        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            private boolean wasOpened;

            private final int DefaultKeyboardDP = 100;

            // From @nathanielwolf answer...  Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
            private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);

            private final Rect r = new Rect();

            @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;
                listener.onVisibilityChanged(isShown);
            }
        });
    }

    public TeamChatView() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment TeamChatView.
     */
    public static TeamChatView newInstance() {
        TeamChatView fragment = new TeamChatView();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(TeamChatPresenter.class, TeamChatModel.class, savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_team_chat_view, container, false);
        this.mUnbinder = ButterKnife.bind(this, view);

        mTestMessageList = new ArrayList<>();
        this.mTeamChatListAdapter = new TeamChatListAdapter(mTestMessageList);
        this.mTeamChatRecyclerView.setAdapter(this.mTeamChatListAdapter);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        this.mTeamChatRecyclerView.setLayoutManager(linearLayoutManager);

        this.mTeamChatSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!String.valueOf(mMessageTextInput.getText()).equals("")) {
                    getSpecificImpOfGenericPresenter().sendMessage(String.valueOf(mMessageTextInput.getText()));
                    mMessageTextInput.setText("");
                    mTeamChatRecyclerView.smoothScrollToPosition(mTestMessageList.size());
                }
            }
        });

        setKeyboardListener(new OnKeyboardVisibilityListener(){
            @Override
            public void onVisibilityChanged(boolean visible) {
                RelativeLayout contentFrame = (RelativeLayout) getActivity().findViewById(R.id.content_company_navigation);
                BottomBar lowerNavigationBar = (BottomBar) getActivity().findViewById(R.id.bottomBar);
                if (visible) { // if more than 100 pixels, its probably a keyboard...
                    lowerNavigationBar.setVisibility(GONE);
                    contentFrame.setPadding(0, 0, 0, 0);
                    mTeamChatRecyclerView.smoothScrollToPosition(mTestMessageList.size());
                } else {
                    contentFrame.setPadding(0, 0, 0, convertDpToPixel(60, getContext()));
                    mTeamChatRecyclerView.smoothScrollToPosition(mTestMessageList.size());
                    lowerNavigationBar.setVisibility(View.VISIBLE);
                }
            }
        });
        return view;
    }

    /**
     * This method converts dp unit to equivalent pixels, depending on device density.
     *
     * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent px equivalent to dp depending on device density
     */
    public static int convertDpToPixel(float dp, Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        int px = (int) (dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }

    public void addToTestMessageList(String str){
        this.mTestMessageList.add(str);
        this.mTeamChatListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
       // getView().getViewTreeObserver().removeGlobalOnLayoutListener(test);
    }

    @Override
    public TeamChatPresenter getSpecificImpOfGenericPresenter() {
        return (TeamChatPresenter) this.mPresenter;
    }
}
这是我的XML布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="uk.cal.codename.projectnedry.TeamChatFragment.TeamChatView">

    <android.support.v7.widget.RecyclerView
        android:layout_above="@+id/chatViewMessageEntryLayout"
        android:id="@+id/teamChatList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:isScrollContainer="false"
        android:paddingTop="10dp"
        android:scrollbars="vertical" />    

    <RelativeLayout
        android:id="@+id/chatViewMessageEntryLayout"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <View
            android:id="@+id/chatViewMessageEntrySeperator"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#e3e3e8" />

        <EditText
            android:id="@+id/messageTextInput"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_below="@+id/chatViewMessageEntrySeperator"
            android:layout_toLeftOf="@+id/teamChatSendButton"
            android:background="@android:color/transparent"
            android:hint="Enter message"
            android:inputType="textCapSentences|textMultiLine"
            android:maxLength="1000"
            android:maxLines="4"
            android:paddingLeft="10dp" />

        <ImageButton
            android:id="@+id/teamChatSendButton"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_gravity="center"
            android:background="#00B9EF"
            android:src="@drawable/ic_send_white_24dp" />

    </RelativeLayout>

</RelativeLayout>

在这里发布您的活动代码。 - Chirag Savsani
请发布您的活动或片段类以及XML。 - Quick learner
你找到解决方案了吗?我也遇到了同样的问题。 - sunil kushwah
14个回答

129

最简单的实现方法,是在 AndroidManifest.xml 中添加

<activity android:windowSoftInputMode="adjustPan"/>

希望这能帮助到某些人。享受吧!


14
在这种模式下,“ToolBar”会从屏幕滑出。 - Konstantin Konopko
7
当键盘出现时,“adjustPan”会隐藏工具栏,而“adjustResize”会显示重叠的底部栏布局。有什么解决方案吗? - Ahamadullah Saikat
可以在主题级别的样式中添加。 - Richard Muvirimi

34

你只需像这样在你的清单文件中添加此代码即可:

 <activity android:name=".MainActivity"
        android:windowSoftInputMode="adjustPan">

这对我有效。愉快编码!


可以在主题级别的样式中添加。 - Richard Muvirimi

16

我最终采用了似乎是软键盘检测标准方法的高度测量法,该方法在这个答案中有描述。但是,我使用了这个库的实现,因为它仍然是同一个ViewTreeObserver.OnGlobalLayoutListener方法,实现得很好,并允许我将代码从我的应用程序主代码库中抽象出来。

当触发此键盘可见性监听器时,我会隐藏/显示底部导航栏(我在这里解释过)。


1
这是最好的答案,因为在一个可回收视图中可能会有一个EditText(或包含EditText的子视图)。因此,您不希望设置adjustPan,因为在这种情况下,键盘可能会覆盖您在可回收视图中的内容。 - Tzegenos

9
在您的ActivityFragment中的onResume()添加此行代码。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

这对我很有效。只需尝试一次。


7

积极监听键盘(IME)的打开/关闭事件,并相应地显示/隐藏底部导航。

我们可以利用 WindowInsetsCompat API,使此工作变得轻松。

步骤1:确保您已迁移到 AndroidX

步骤2:在您的 Fragment 中的 onCreateView 中添加键盘(IME)事件的监听器


ViewCompat.setOnApplyWindowInsetsListener(window.decorView.rootView) { _, insets ->
 
   //This lambda block will be called, every time keyboard is opened or closed

    
    val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
    if(imeVisible){ 
     //Now show-hide bottom navigation accordingly 
    } 

    insets
}

这就是您所需的全部。

如需了解有关窗口嵌入内容更多信息,您可以查看这篇深入的文章

注意:
早期检测键盘打开/关闭事件并不容易,Android开发人员曾尝试各种黑科技来实现此功能。但在数十年的请求、祈祷和抱怨之后,Google终于推出了WindowInsets API。感谢Google!

1
这个监听器似乎在键盘上方添加了额外的填充,如何避免它? - Michael Abyzov
抱歉,伙计,我没有遇到过这种情况。事件监听器很少会向您的视图边界添加填充。也许是其他视图,您可以使用Android Studio的布局检查器检查您的视图层次结构。 - iCantC
1
我猜这是因为我的屏幕底部有额外的Spacer,并且在我的活动中设置了adjustResize标志,这将键盘抬起一部分间距... - Michael Abyzov

6

只需在AndroidManifest.xml文件中的每个Activity中添加下面的属性。

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />

1
如果您想为应用程序设置默认模式,请考虑在主题中使用属性 <item name="android:windowSoftInputMode">stateAlwaysHidden|adjustPan</item> 进行设置。 - Peter F

4

针对API 21及以上版本:

首先,在AndroidManifest.xml文件中为该活动设置"android:windowSoftInputMode"属性,将其值设为"adjustResize"

其次,在您的活动的onCreate方法中注册以下监听器:

window.decorView.setOnApplyWindowInsetsListener { view, insets ->
    val insetsCompat = toWindowInsetsCompat(insets, view)
    val isImeVisible = insetsCompat.isVisible(WindowInsetsCompat.Type.ime())
    // below line, do the necessary stuff:
    dataBinding.bottomNavigation.visibility = if (isImeVisible) View.GONE else View.VISIBLE
    view.onApplyWindowInsets(insets)
}

1
使用Compat库的第一行是ViewCompat.setOnApplyWindowInsetsListener(window.decorView)...,最后一行是ViewCompat.onApplyWindowInsets(v, insets) - Miklos Jakab

3
 private boolean keyboardListenersAttached = false;
private ViewGroup rootLayout;


       @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_settings, container, false);


    rootLayout = (ViewGroup) view.findViewById(R.id.settings_layout);
    attachKeyboardListeners();

    return view;
}



    private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new 
    ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = rootLayout.getRootView().getHeight() - 
        rootLayout.getHeight();
        if (getActivity() != null) {
            int contentViewTop = 
           getActivity().getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
            LocalBroadcastManager broadcastManager = 
            LocalBroadcastManager.getInstance(getContext());
            Rect r = new Rect();
            rootLayout.getWindowVisibleDisplayFrame(r);
            int screenHeight = rootLayout.getRootView().getHeight();

            // r.bottom is the position above soft keypad or device button.
            // if keypad is shown, the r.bottom is smaller than that before.
            int keypadHeight = screenHeight - r.bottom;
            if (keypadHeight > screenHeight * 0.15) {
                onHideKeyboard();

                Intent intent = new Intent("KeyboardWillHide");
                broadcastManager.sendBroadcast(intent);
            } else {
                int keyboardHeight = heightDiff - contentViewTop;
                onShowKeyboard(keyboardHeight);

                Intent intent = new Intent("KeyboardWillShow");
                intent.putExtra("KeyboardHeight", keyboardHeight);
                broadcastManager.sendBroadcast(intent);
            }
        }else {

        }
    }
};

protected void onShowKeyboard(int keyboardHeight) {
    System.out.println("keyboard is shown");
    dashboardActivity.bottomNavigationView.setVisibility(View.VISIBLE);
    dashboardActivity.fab.setVisibility(View.VISIBLE);

}
protected void onHideKeyboard() {
    System.out.println("keyboard is hide");
    dashboardActivity.bottomNavigationView.setVisibility(View.INVISIBLE);
    dashboardActivity.fab.setVisibility(View.INVISIBLE);
}

protected void attachKeyboardListeners() {
    if (keyboardListenersAttached) {
        return;
    }

    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
    keyboardListenersAttached = true;
}

在 Manifest.xml 文件中添加。
<activity
        android:name=".Activity.DashboardActivity"
        android:windowSoftInputMode="adjustResize"

         />

2
在您的清单文件中activity标签内添加属性:android:windowSoftInputMode="adjustResize"
 <activity
        android:name=".YourActivity"
        android:windowSoftInputMode="adjustResize"/>

注意:我建议您将NestedScrollView用作父布局。

希望这可以帮到您。


我已经尝试过这个,它会使我的操作栏与前三到四条消息一起消失在屏幕外。 - Calco
你尝试过移除测量方法吗?你可以试着注释掉之前与此问题相关的代码。 - tahsinRupam
使用NestedScrollView为单个RecycleView有什么意义?您可以在NSV中包含一个父布局,其中可以添加RecycleView和RelativeLayout(chatViewMessageEntryLayout)。 - tahsinRupam
我移除了NSV,现在只剩下RecycleView。这是使用adjustPan的效果,你可以看到操作栏消失了,只有一半的EditTest可见。 - Calco
请检查我的更新答案。使用adjustResize代替。否则,您应该将所有视图包装在scrollView中,否则键盘将没有空间显示。 - tahsinRupam
很遗憾,当底部导航栏出现/隐藏时,我仍然会看到闪烁。 - Calco

2
在我的情况下,我使用DrawerLayout作为父视图,并添加了一些布局内容和底部导航栏。在Manifest文件中,通过在Activity中添加"android:windowSoftInputMode="adjustPan"这个标签,可以使其正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接