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
        }
}
复制代码
相关推荐
爱笑鱼1 天前
Binder(八):远端进程死了,BinderProxy 为什么还能收到通知?
android
Android-Flutter1 天前
Kotlin 冷流与热流详解
android·kotlin
zhangphil1 天前
Android ContentProvider/ContentResolver读图片,跨进程 IPC慢
android
yueqc11 天前
Android .so 文件压缩
android·so·包体积
龚礼鹏1 天前
深入解析 Android Automotive (AAOS) 启动流程与 CarService 核心机制(基于 Android 16 最新源码视角)
android
rosmis1 天前
agent各指标定义
android·java·开发语言
风样滴男人哟1 天前
PHP特性之反射类ReflectionClass机制
android·开发语言·php
小孔龙1 天前
RenderNode 与 DisplayList:Android 硬件加速的绘制记录与复用
android·性能优化
qizayaoshuap1 天前
鸿蒙 ArkTS 实战:搜索列表 城市实时过滤(示例 94)
android·华为·harmonyos
朝星1 天前
Framework筑基之Handler消息机制5(应用层)
android