当前位置: 首页 > news >正文

Android 解决键盘遮挡输入框

本文目录 点击直达

  • Android 解决键盘遮挡输入框
    • 代码实现
    • 使用
      • 注意
  • 最后我还有一句话要说
      • 梧桐叶上三更雨,叶叶声声是别离。

Android 解决键盘遮挡输入框

在安卓中通常可以通过添加android:windowSoftInputMode="adjustResize|stateHidden"的方式来让键盘顶起布局,但是如果对状态栏进行过着色隐藏等操作时,这个配置将不会生效,此时输入框输入时键盘仍然不会将布局抬起

经过一番搜索和验证,可以使用AndroidBug5497Workaround来解决问题,但是现今此方案已无法完美适配底部导航栏的情况,所以我基于之前的方案进行了优化

代码实现

将AdjustResizeHelper.kt类Copy进你的项目,需要注意的是这是kotlin语法

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.WindowInsetsobject AdjustResizeHelper {fun supportAdjustResize(activity: Activity) {val decorView = activity.window.decorViewvar usableHeightPrevious = 0if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {decorView.setOnApplyWindowInsetsListener { v, insets ->val usableHeightNow = computeUsableHeight(decorView)if (usableHeightPrevious == usableHeightNow) {return@setOnApplyWindowInsetsListener insets}usableHeightPrevious = usableHeightNowval imeInsets = insets.getInsets(WindowInsets.Type.ime())val navigationBars = insets.getInsets(WindowInsets.Type.navigationBars())val keyboardHeight = imeInsets.bottom - navigationBars.bottomif (keyboardHeight > 0) {decorView.setPadding(0, 0, 0, keyboardHeight)} else {decorView.setPadding(0, 0, 0, 0)}v.onApplyWindowInsets(insets)}} else {decorView.viewTreeObserver.addOnGlobalLayoutListener {val usableHeightNow = computeUsableHeight(decorView)if (usableHeightPrevious == usableHeightNow) {return@addOnGlobalLayoutListener}usableHeightPrevious = usableHeightNowval rect = Rect()decorView.getWindowVisibleDisplayFrame(rect)val screenHeight = decorView.heightval heightDifference = screenHeight - rect.bottom - getNavigationBarHeight(activity)if (heightDifference > 100) { // 软键盘弹出decorView.setPadding(0, 0, 0, heightDifference)} else { // 软键盘隐藏decorView.setPadding(0, 0, 0, 0)}}}}@SuppressLint("InternalInsetResource")fun getNavigationBarHeight(context: Context): Int {val resources = context.resourcesval resourceId = resources.getIdentifier("navigation_bar_height","dimen","android")return if (resourceId > 0) {resources.getDimensionPixelSize(resourceId)} else 0}private fun computeUsableHeight(view: View): Int {val r = Rect()view.getWindowVisibleDisplayFrame(r)return (r.bottom - r.top)}
}

使用

使用起来很简单,先在Activity的配置中添加android:windowSoftInputMode="adjustResize|stateHidden",然后如下图在Activity的onCreate回调中添加此功能即可
在这里插入图片描述

注意

因为使用通话的双通道麦克风实现了降噪,所以使用时可能声音较小,如果没有声音,请将声音调到最大,然后凑近麦克风吼两句"感谢博主,我会一键三连的"

最后我还有一句话要说

梧桐叶上三更雨,叶叶声声是别离。

周紫芝《鹧鸪天·一点残红欲尽时》

http://www.lryc.cn/news/604133.html

相关文章:

  • 老年护理实训室建设方案:打造安全、规范、高效的实践教学核心平台
  • C++ 编程问题记录
  • 对象的创建过程
  • Linux学习--C语言(指针4、结构体)
  • 【Spring】日志级别的分类和使用
  • Qt小技巧 QStandardPaths详解
  • C语言14-指针4-二维数组传参、指针数组传参、viod*指针
  • JAVA_TWENTY—ONE_单元测试+注解+反射
  • 在 Cloudflare 平台上完整部署 GitHub 项目 MoonTV 实现免费追剧流程
  • vite + chalk打印输出彩色命令行
  • 并查集介绍及典型应用和编程题
  • Python爬虫01_Requests第一血获取响应数据
  • __getattr__和 __getattribute__ 的用法
  • Docker学习相关视频笔记(二)
  • linux内核报错汇编分析
  • 云原生周刊:2025年的服务网格
  • JSON-RPC 2.0 规范
  • fastjson反序列化时_id的处理
  • WebRTC 2025全解析:从技术原理到商业落地
  • MC0241防火墙
  • 16大工程项目管理系统对比:开源与付费版本
  • 牛客网之华为机试题:密码验证程序
  • python-网络编程
  • Qt 移动应用性能优化策略
  • 板凳-------Mysql cookbook学习 (十二--------7)
  • Android User版本默认用test-keys,如何改用release-keys
  • 北方公司面试记录
  • 前端数据库:IndexedDB从基础到高级使用指南
  • 基于Prophet、滑动平均、加权平均的地铁客流量预测与可视化系统的设计与实现
  • Java【代码 17】httpclient PoolingHttpClientConnectionManager 连接池使用举例