EditText被键盘遮挡无法输入求助(含RecyclerView布局)
问题:键盘覆盖输入框,移除RecyclerView后恢复正常
点击etMessage输入框时,弹出的键盘会完全覆盖它,导致看不到输入内容。删掉rvChat这个RecyclerView后问题消失,但这个控件是聊天页面必须的。试过设置android:windowSoftInputMode="adjustResize|stateHidden",没用。布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ChatActivity" > <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_constraintTop_toTopOf="parent"> <ImageButton android:id="@+id/btnBack" android:layout_width="38dp" android:layout_height="38dp" android:background="@drawable/arrow_back" android:layout_gravity="start" android:layout_marginStart="10dp" /> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginStart="15dp" android:gravity="center_vertical" android:textColor="@color/white" android:textSize="24sp" android:textStyle="bold" /> </androidx.appcompat.widget.Toolbar> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvChat" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="@+id/etMessage" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintVertical_bias="0.0" tools:layout_editor_absoluteX="0dp" /> <EditText android:id="@+id/etMessage" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/et_style" android:hint="Message" android:inputType="text" android:padding="15dp" android:layout_margin="10dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="@+id/rvChat" /> <ImageView android:id="@+id/btnVoiceMessage" android:layout_width="38dp" android:layout_height="38dp" android:background="@drawable/voice_message_btn" app:layout_constraintBottom_toBottomOf="@+id/etMessage" app:layout_constraintEnd_toStartOf="@+id/btnSendMessage" app:layout_constraintTop_toTopOf="@+id/etMessage" android:layout_marginEnd="10dp" /> <ImageView android:id="@+id/btnSendMessage" android:layout_width="38dp" android:layout_height="38dp" android:background="@drawable/send_message_btn" app:layout_constraintBottom_toBottomOf="@+id/etMessage" app:layout_constraintEnd_toEndOf="@+id/etMessage" app:layout_constraintTop_toTopOf="@+id/etMessage" android:layout_marginEnd="10dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
解决方法
1. 修正RecyclerView的布局约束
当前RecyclerView用wrap_content作为高度,再加上constraintVertical_bias="0.0",会让布局计算逻辑混乱。改成0dp(匹配约束),并移除垂直bias:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/rvChat" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/etMessage" app:layout_constraintTop_toBottomOf="@id/toolbar" tools:layout_editor_absoluteX="0dp" />
这样RecyclerView会自动占满Toolbar和输入框之间的所有空间,系统调整键盘布局时能正确识别输入框的位置。
2. 确认Activity的软键盘配置
在AndroidManifest.xml里对应Activity标签下添加:
android:windowSoftInputMode="adjustResize|stateHidden"
如果用了全屏主题,adjustResize可能失效,需要在Activity的onCreate里额外设置:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
同时检查根布局的fitsSystemWindows属性,确保是true:
android:fitsSystemWindows="true"
3. 添加RecyclerView滚动监听(可选优化)
键盘弹出时自动滚动到最新消息位置,既避免遮挡,也符合聊天场景的用户习惯:
rvChat.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { if (bottom < oldBottom) { rvChat.postDelayed(() -> { if (adapter.getItemCount() > 0) { rvChat.scrollToPosition(adapter.getItemCount() - 1); } }, 100); } } });
内容的提问来源于stack exchange,提问作者mrprikol
相关产品推荐
相关产品推荐

