外接键盘时TextInputEditText自动失焦(Android12+)求助
Android 12+ 外接键盘时评论框输入一次后焦点跳转到返回按钮问题
问题描述
我实现了一个CardView适配器,每个卡片包含必填值输入框与可选评论框,二者均由TextInputLayout包裹TextInputEditText。代码中为两个输入框设置了TextWatcher:
- 值输入框验证通过后启用评论框,并将输入值存入对象(
object.setValue();) - 评论框仅将输入内容存入同一对象(
object.setComment();)
UI代码
<TextView android:id="@+id/txt_value_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Value:*" android:textAppearance="@style/Bold.FontPath" android:textSize="@dimen/cardview_textsize_header" android:textStyle="bold" /> <com.google.android.material.textfield.TextInputLayout android:id="@+id/layout_edt_value" android:layout_width="match_parent" android:layout_height="wrap_content" app:endIconMode="clear_text"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/edt_value" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="@dimen/twenty_dp" android:background="@drawable/rounded_border" android:inputType="text" android:lines="1" android:maxLines="1" android:textColor="@color/black" android:textSize="@dimen/cardview_textsize_header" /> </com.google.android.material.textfield.TextInputLayout> <TextView android:id="@+id/txt_commentbox_title" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="@dimen/ten_dp" android:text="Title" android:textAppearance="@style/Bold.FontPath" android:textSize="@dimen/cardview_textsize_header" android:textStyle="bold" /> <com.google.android.material.textfield.TextInputLayout android:id="@+id/layout_edt_comment" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="255" app:endIconMode="clear_text"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/edt_comment" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="2" android:gravity="top" android:background="@drawable/rounded_border" android:inputType="textMultiLine|textCapSentences" android:maxEms="255" android:maxLength="255" android:maxLines="3"/> </com.google.android.material.textfield.TextInputLayout>
异常现象
外接键盘时,值输入框工作正常,但聚焦评论框后按一次按键,焦点会自动跳转到顶部返回按钮。该问题仅在Android 12及以上版本出现,低版本无异常。
日志警告(仅评论框EditText出现)
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
已尝试的操作
- 修改UI布局结构
- 改用普通
EditText替代TextInputEditText - 将
com.google.android.material:material:1.3.0升级至最新版本 - 移除所有触发输入框状态的代码逻辑
- 新增空白EditText可正常工作,但原评论框问题依旧
解决方案建议
1. 移除自定义背景冲突
TextInputLayout本身提供了完整的边框样式,自定义android:background可能在Android 12+中与系统输入焦点管理冲突。尝试移除评论框EditText的自定义背景,改用TextInputLayout的内置样式:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/layout_edt_comment" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="255" app:endIconMode="clear_text" app:boxBackgroundMode="outline" app:boxCornerRadiusBottomEnd="8dp" app:boxCornerRadiusBottomStart="8dp" app:boxCornerRadiusTopEnd="8dp" app:boxCornerRadiusTopStart="8dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/edt_comment" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="2" android:gravity="top" android:inputType="textMultiLine|textCapSentences" android:maxLength="255" android:maxLines="3"/> </com.google.android.material.textfield.TextInputLayout>
2. 强制维护InputConnection有效性
Android 12+对InputConnection管理更严格,可在评论框获取焦点时强制重建连接:
edt_comment.setOnFocusChangeListener((v, hasFocus) -> { if (hasFocus) { v.post(() -> { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT); }); } });
3. 禁用自动填充干扰
Android 12+的自动填充服务可能干扰焦点,给评论框添加以下属性:
android:importantForAutofill="no" android:autofillHints=""
4. 检查RecyclerView动画(若适用)
如果CardView在RecyclerView中,Android 12+对Item动画和焦点的兼容性更严格,尝试禁用动画:
recyclerView.setItemAnimator(null);
5. 排查TextWatcher逻辑
再次确认评论框的TextWatcher中是否存在间接触发焦点变化的代码,比如意外调用clearFocus()或触发视图刷新导致焦点丢失。
内容的提问来源于stack exchange,提问作者Rangnitt
相关产品推荐
相关产品推荐

