Android软键盘遮挡/动画最佳解决方案

5,454

文章内容

原文地址: juejin.cn/post/703842…

本文已获得原作者同意转载


附赠DLC

源码地址: soft-input-event

Android软键盘最佳解决方案

下载体验


不同于其他同类型框架, 本框架使用Android11推出的新功能WindowInsetsAnimation来解决软键盘相关问题, 具备软键盘平滑过渡动画(部分机型可能不支持), 提供更准确的软键盘Api以及软键盘遮挡解决方案


欢迎贡献代码/问题

特点

  • 软键盘显示隐藏使用平滑动画过渡
  • 指定视图悬浮于软键盘上
  • 更准确的软键盘高度/显示隐藏监听
  • 指定监听输入框
  • 显示/隐藏软键盘
  • 支持对话框
  • 更简单易用的方法

使用

监听键盘显示或者指定视图悬浮于软键盘上只需要一个方法

本方法支持Activity/Fragment/Dialog/DialogFragment/BottomSheetDialogFragment

setWindowSoftInput(
    float = llInput,
    onChanged = {
        Log.d("SoftInput", "visibility = ${hasSoftInput()}")
    }
)
方法描述
setWindowSoftInput软键盘遮挡/监听显示隐藏
showSoftInput/hideSoftInput显示隐藏软键盘
hasSoftInput当前是否有软键盘
getSoftInputHeight当前软键盘高度

安装

添加远程仓库根据创建项目的 Android Studio 版本有所不同

Android Studio Arctic Fox以下创建的项目 在项目根目录的 build.gradle 添加仓库

allprojects {
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}

Android Studio Arctic Fox以上创建的项目 在项目根目录的 settings.gradle 添加仓库

dependencyResolutionManagement {
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}

然后在 module 的 build.gradle 添加依赖框架

implementation 'com.github.liangjingkanji:soft-input-event:1.0.5'

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
项目描述
Net🍉 Android 最强大的创新式协程并发网络请求 GitHub Repo stars
BRV🌽 Android 最强大的RecyclerView库, 比BRVAH更优雅和强大 GitHub Repo stars
soft-input-eventAndroid软键盘遮挡/平滑动画最佳解决方案 GitHub Repo stars
spannableAndroid最好的Spannable构建工具, 首个支持正则匹配/GIF动画/图文混排 GitHub Repo stars
IntervalAndroid计时器工具, 倒计时/正计时/开始/暂停/继续/结束/取消 GitHub Repo stars
Serialize应用数据存储的神器, 比SQLite/SP更方便, 可创建自动本地读写/应用销毁恢复的字段, GitHub Repo stars
StateLayout一行代码构建整个应用的缺省页 GitHub Repo stars
StatusBar🍥 Android 一行代码配置透明状态栏 GitHub Repo stars
Channel优雅的事件消息框架 🍯 LiveData / Coroutine / 生命周期 特性 EventBus GitHub Repo stars
TooltipToast(吐司)BubbleDialog(菊花加载框) GitHub Repo stars
debugKit开发调试悬浮窗口工具 GitHub Repo stars
LogCat日志输出工具 GitHub Repo stars
Engine支持快速开发的基础库 GitHub Repo stars
AutoDispose一个函数自动解绑RxJava订阅, 比RxLifeCycle更简单
RxBus支持 标签事件/线程切换/kotlin特性/无需注册注销 的 RxBus