Android 底部EditView输入时悬浮到软键盘上方

1. 修改 Activity 的 Manifest 配置

确保你的 Activity 在 AndroidManifest.xml 中有以下配置:

XML 复制代码
<activity
    android:name=".YourActivity"
    android:windowSoftInputMode="adjustResize|stateHidden" 
/>

关键点:

  • adjustResize 是关键属性,使布局会为键盘腾出空间

  • stateHidden 可选,表示初始时不自动弹出键盘

2. 更新布局文件

使用 CoordinatorLayout 实现最可靠的效果:

XML 复制代码
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">


    <!-- 界面中的其余布局 -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="72dp" /> <!-- 留出输入框高度的空间 -->


 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        android:orientation="horizontal"
        android:padding="8dp">

        <EditText
            android:id="@+id/etMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="输入消息..."
            android:imeOptions="actionSend"
            android:inputType="textCapSentences|textMultiLine" />

        <ImageButton
            android:id="@+id/btnSend"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:src="@drawable/ic_send"
            android:background="?attr/selectableItemBackgroundBorderless" />
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

3. 确保主题配置正确

styles.xml 中,确保 Activity 主题不是全屏模式:

XML 复制代码
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- 不要设置以下属性,否则会失效 -->
    <!-- <item name="android:windowFullscreen">true</item> -->
</style>

4. 添加自动滚动功能(可选但推荐)

在代码中添加:

Kotlin 复制代码
private fun setupKeyboardBehavior() {
    binding.root.viewTreeObserver.addOnGlobalLayoutListener {
        val rect = Rect()
        binding.root.getWindowVisibleDisplayFrame(rect)
        val screenHeight = binding.root.height
        val keypadHeight = screenHeight - rect.bottom
        
        if (keypadHeight > screenHeight * 0.15) { // 键盘显示
            binding.recyclerView.post {
                val lastPosition = (binding.recyclerView.adapter?.itemCount ?: 0) - 1
                if (lastPosition >= 0) {
                    binding.recyclerView.smoothScrollToPosition(lastPosition)
                }
            }
        }
    }
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    setupKeyboardBehavior()
    // ...其他初始化代码...
}

注意:enableEdgeToEdge失效问题

当布局使用enableEdgeToEdge(界面延申到通知栏的)时enableEdgeToEdge会失效

解决办法:

Kotlin 复制代码
  public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        //解决enableEdgeToEdge与fitsSystemWindows= false时的冲突
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content)) { view, insets ->
           val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
           //binding.content就是界面的最外层layout
           binding.content.updatePadding(top = -systemBars.top)
           insets
        }
}
复制代码
相关推荐
CIb0la3 小时前
安卓16系统升级后(Google pixel 8/8pro 9/9pro xl 10/10pro xl)救砖及Root方法
android·运维·生活
Ya-Jun4 小时前
项目实战Now in Android:项目模块说明
android·架构·kotlin
@Aurora.5 小时前
【MySQL】基础
android
ooooooctober5 小时前
PHP代码审计框架性思维的建立
android·开发语言·php
q***82916 小时前
图文详述:MySQL的下载、安装、配置、使用
android·mysql·adb
沐怡旸6 小时前
【底层机制】Ashmem匿名共享内存:原理与应用深度解析
android·面试
用户2018792831677 小时前
Activity结束动画与System.exit(0)的黑屏之谜
android
Proud lion8 小时前
Apipost 脚本高频场景最佳实践:搞定接口签名验证、登录令牌刷新、动态参数生成等
android
介一安全8 小时前
【Frida Android】实战篇5:SSL Pinning 证书绑定绕过 Hook 教程(二)
android·网络安全·逆向·安全性测试·frida
2501_937193148 小时前
PLB-TV 影视!无广告 + 4K 高清
android·源码·源代码管理·机顶盒