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

解决Android软键盘遮挡EditText输入框的问题

问题原因与解决方案

你的adjustResize未生效的核心原因是**enableEdgeToEdge()配合自定义的WindowInsets监听器,只处理了系统栏的边距,完全忽略了软键盘(IME)的Insets变化**,导致ScrollView无法感知软键盘弹出,自然不会调整布局大小。此外,根布局的fitsSystemWindows="true"会干扰Insets的传递逻辑,进一步阻止了软键盘的resize响应。


修复步骤

1. 修改Activity的Insets处理逻辑

更新OnApplyWindowInsetsListener,同时处理系统栏和软键盘的Insets,让ScrollView能感知软键盘的高度变化:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        
        val mainLayout = findViewById<LinearLayout>(R.id.main)
        val scrollView = findViewById<ScrollView>(R.id.nested_scroll_view)
        
        ViewCompat.setOnApplyWindowInsetsListener(mainLayout) { v, insets ->
            // 处理系统栏边距,保证标题栏不被系统栏遮挡
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            
            // 处理软键盘边距,给ScrollView底部留出键盘高度
            val imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime())
            scrollView.setPadding(scrollView.paddingLeft, scrollView.paddingTop, scrollView.paddingRight, imeInsets.bottom)
            
            insets
        }
    }
}

2. 调整布局文件

移除根布局的fitsSystemWindows="true",避免它干扰Insets的传递:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <TextView
        android:id="@+id/mytitle"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="Static Header"
        android:gravity="center_horizontal"
        android:textSize="24sp"/>
    
    <ScrollView
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            
            <EditText
                android:layout_width="match_parent"
                android:text="item"
                android:inputType="textLongMessage"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content"/>
            <!-- 重复39个EditText -->
            
        </LinearLayout>
    </ScrollView>
</LinearLayout>

3. 保留Manifest配置

确保android:windowSoftInputMode="adjustResize"的设置不变,这是触发布局resize的基础。


原理说明

  • enableEdgeToEdge()会让窗口内容延伸到系统栏下方,此时必须手动处理所有类型的Insets(系统栏、软键盘等),默认的adjustResize逻辑才会生效。
  • 给ScrollView单独设置软键盘的底部padding,相当于告诉ScrollView:"软键盘弹出时,你底部要留出这么多空间",这样ScrollView会自动调整内部布局的可见区域,让被点击的EditText滚动到软键盘上方。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 06:57:23