You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

软键盘遮挡EditText/TextInputEditText控件底部的解决求助

软键盘截断带自定义背景的EditText/TextInputEditText底部区域的解决办法

我最近碰到一个头疼的问题:当我的EditText/TextInputEditText控件使用了自定义drawable作为边框背景时,弹出软键盘后控件的底部区域会被截断;但如果去掉这个背景drawable,控件就能正常完整地显示在软键盘上方。

我先后尝试了三种方案,结果都没解决问题:

  • 方案1:重写setBackgroundResource方法,试图保留控件原有padding,避免背景设置影响布局
    @Override
    public void setBackgroundResource(int resid) {
        int pl = getPaddingLeft();
        int pt = getPaddingTop();
        int pr = getPaddingRight();
        int pb = getPaddingBottom();
        super.setBackgroundResource(resid);
        this.setPadding(pl, pt, pr, pb);
    }
    
  • 方案2:在AndroidManifest对应的Activity中设置android:windowSoftInputMode="adjustResize"
  • 方案3:改用android:windowSoftInputMode="adjustPan"配置

补充布局及样式代码

后来我把相关布局和样式片段整理出来,方便排查问题:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint=" " >
    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="0px"
        android:layout_height="0px"/>
    <android.support.design.widget.TextInputEditText
        android:id="@+id/txtUserName"
        android:layout_width="match_parent"
        android:layout_marginBottom="@dimen/textControlMarginBottom"
        android:hint="@string/userNameHint"
        android:inputType="text"
        android:nextFocusUp="@id/txtUserName"
        android:nextFocusLeft="@id/txtUserName"
        android:nextFocusDown="@+id/txtPassword"
        android:nextFocusRight="@+id/txtPassword"
        style="@style/EditTextViewSingleRowStyle" />
</android.support.design.widget.TextInputLayout>

<style name="EditTextViewSingleRowStyle">
    <item name="android:layout_height">@dimen/textViewHeight</item>
    <item name="android:paddingTop">@dimen/textViewTopPadding</item>
    <item name="android:paddingBottom">@dimen/textViewBottomPadding</item>
    <item name="android:background">@drawable/selector_text_control_edit</item>
    <item name="android:cursorVisible">true</item>
    <item name="android:fontFamily">sans-serif</item>
    <item name="android:gravity">center_vertical</item>
    <item name="android:maxLines">1</item>
    <item name="android:singleLine">true</item>
    <item name="android:textColor">@color/controlTextColor</item>
    <item name="android:textColorHint">@color/controlTextViewHint</item>
    <item name="android:textCursorDrawable">@drawable/cursor</item>
    <item name="android:textIsSelectable">true</item>
    <item name="android:textSize">@dimen/controlTextSize</item>
</style>

最终解决方案

折腾了好一阵才发现问题根源:我在样式里给EditText设置了固定高度(@dimen/textViewHeight实际对应48dp),当软键盘弹出时,系统调整布局时无法正确计算控件的实际显示高度,导致底部被截断。

把样式里的android:layout_height='48dp'改成android:layout_height='match_content'之后,软键盘弹出时控件就能完整显示在上方了!

内容的提问来源于stack exchange,提问作者Orson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:07:09