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
        }
}
复制代码
相关推荐
mmsx7 分钟前
MapLibre 实战 09|Bug 单写着"地图被风刮走了":一个瓦片源工厂,是三个线上事故换来的
android·前端·开源
一技安身25 分钟前
【信创】统信UOS 银河麒麟离线部署Python3.11完整方案
android·java·python3.11
墨狂之逸才1 小时前
Android WebView 初始缩放:什么时候该设置 100%,什么时候不要设置
android
淡淡的香烟1 小时前
AndroidKMP之导航和WebView
android
天空之城--13 小时前
Android逆向安全合规接单指南
android·安全
消失的旧时光-194315 小时前
Android 系统层扫盲 09:AMS、ATMS、WMS、PMS 到底分别管什么?
android·wms·pms·ams·aosp·atms
Godikov15 小时前
Android 系统级设备应用踩坑实录:sharedUserId 签名、SDK 授权失败 -4、开机自启与 U 盘 OTA 升级
android
IT毕设实战小研18 小时前
基于大数据的国内主要农作物产量趋势分析与可视化
android·java·大数据·django·课程设计
企业数字化笔记20 小时前
固定资产历史数据怎么导入系统?Excel模板、字段映射和数据校验
android·java·数据库·后端
龙之叶20 小时前
Android出海系列-GMS认证介绍
android