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

打印 上一主题 下一主题

主题 1976|帖子 1976|积分 5928

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1. 修改 Activity 的 Manifest 配置

确保你的 Activity 在 AndroidManifest.xml 中有以下配置:
  1. <activity
  2.     android:name=".YourActivity"
  3.     android:windowSoftInputMode="adjustResize|stateHidden"
  4. />
复制代码
关键点:


  • adjustResize 是关键属性,使结构会为键盘腾出空间
  • stateHidden 可选,表现初始时不主动弹出键盘
2. 更新结构文件

利用 CoordinatorLayout 实现最可靠的效果:
  1. <androidx.coordinatorlayout.widget.CoordinatorLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:fitsSystemWindows="true">
  7.     <!-- 界面中的其余布局 -->
  8.     <androidx.recyclerview.widget.RecyclerView
  9.         android:id="@+id/recyclerView"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="match_parent"
  12.         android:clipToPadding="false"
  13.         android:paddingBottom="72dp" /> <!-- 留出输入框高度的空间 -->
  14. <LinearLayout
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content"
  17.         android:layout_gravity="bottom"
  18.         android:background="@android:color/white"
  19.         android:orientation="horizontal"
  20.         android:padding="8dp">
  21.         <EditText
  22.             android:id="@+id/etMessage"
  23.             android:layout_width="0dp"
  24.             android:layout_height="wrap_content"
  25.             android:layout_weight="1"
  26.             android:hint="输入消息..."
  27.             android:imeOptions="actionSend"
  28.             android:inputType="textCapSentences|textMultiLine" />
  29.         <ImageButton
  30.             android:id="@+id/btnSend"
  31.             android:layout_width="48dp"
  32.             android:layout_height="48dp"
  33.             android:src="@drawable/ic_send"
  34.             android:background="?attr/selectableItemBackgroundBorderless" />
  35.     </LinearLayout>
  36. </androidx.coordinatorlayout.widget.CoordinatorLayout>
复制代码

3. 确保主题配置正确

在 styles.xml 中,确保 Activity 主题不是全屏模式:
  1. <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
  2.     <!-- 不要设置以下属性,否则会失效 -->
  3.     <!-- <item name="android:windowFullscreen">true</item> -->
  4. </style>
复制代码
4. 添加主动滚动功能(可选但推荐)

在代码中添加:
  1. private fun setupKeyboardBehavior() {
  2.     binding.root.viewTreeObserver.addOnGlobalLayoutListener {
  3.         val rect = Rect()
  4.         binding.root.getWindowVisibleDisplayFrame(rect)
  5.         val screenHeight = binding.root.height
  6.         val keypadHeight = screenHeight - rect.bottom
  7.         
  8.         if (keypadHeight > screenHeight * 0.15) { // 键盘显示
  9.             binding.recyclerView.post {
  10.                 val lastPosition = (binding.recyclerView.adapter?.itemCount ?: 0) - 1
  11.                 if (lastPosition >= 0) {
  12.                     binding.recyclerView.smoothScrollToPosition(lastPosition)
  13.                 }
  14.             }
  15.         }
  16.     }
  17. }
  18. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  19.     super.onViewCreated(view, savedInstanceState)
  20.     setupKeyboardBehavior()
  21.     // ...其他初始化代码...
  22. }
复制代码
注意:enableEdgeToEdge失效问题

当结构利用enableEdgeToEdge(界面延申到通知栏的)时enableEdgeToEdge会失效
解决办法:
  1.   public override fun onCreate(savedInstanceState: Bundle?) {
  2.         super.onCreate(savedInstanceState)
  3.         enableEdgeToEdge()
  4.         setContentView(binding.root)
  5.         //解决enableEdgeToEdge与fitsSystemWindows= false时的冲突
  6.         ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content)) { view, insets ->
  7.            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
  8.            //binding.content就是界面的最外层layout
  9.            binding.content.updatePadding(top = -systemBars.top)
  10.            insets
  11.         }
  12. }
复制代码

[code][/code]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表