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 小时前
安卓前后端连接教程
android
氦客2 小时前
Android Compose中的附带效应
android·compose·effect·jetpack·composable·附带效应·side effect
雨白2 小时前
Kotlin 协程的灵魂:结构化并发详解
android·kotlin
我命由我123452 小时前
Android 开发问题:getLeft、getRight、getTop、getBottom 方法返回的值都为 0
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
Modu_MrLiu2 小时前
Android实战进阶 - 用户闲置超时自动退出登录功能详解
android·超时保护·实战进阶·长时间未操作超时保护·闲置超时
Jeled3 小时前
Android 网络层最佳实践:Retrofit + OkHttp 封装与实战
android·okhttp·kotlin·android studio·retrofit
信田君95273 小时前
瑞莎星瑞(Radxa Orion O6) 基于 Android OS 使用 NPU的图片模糊查找APP 开发
android·人工智能·深度学习·神经网络
tangweiguo030519873 小时前
Kotlin 实现 Android 网络状态检测工具类
android·网络·kotlin
nvvas4 小时前
Android Studio JAVA开发按钮跳转功能
android·java·android studio