KeyboardListener
:musical_keyboard: 监听Android软键盘弹出与收回事件(并回调"键盘是否弹出"、"键盘高度"、"屏幕可用高度"、"屏幕宽度数值")
Install / Use
/learn @relish-wang/KeyboardListenerREADME
<img src="./art/title.png" alt="KeyboardListener" height="48" align="bottom" />

从facebook/react-native源码中抽离出来的用于监听Android软键盘弹出与收回事件的相关代码。
关于作者
景三,程序员,主要从事Android平台基础架构方面的工作,欢迎交流技术方面的问题,可以去我的Github提issue或者发邮件至relish.wang@gmail.com与我交流。
[TOC]
扫描二维码下载Demo

一、效果演示


二、使用方法
可以直接把DisplayMetricsHolder、GlobalLayoutListener、OnKeyboardChangedListener三个文件直接拷贝到你的工程里使用。
rootView.getViewTreeObserver().addOnGlobalLayoutListener(
new GlobalLayoutListener(rootView, new OnKeyboardChangedListener() {
/**
* 键盘事件
*
* @param isShow 键盘是否展示
* @param keyboardHeight 键盘高度(当isShow为false时,keyboardHeight=0)
* @param screenWidth 屏幕宽度
* @param screenHeight 屏幕可用高度(不包含底部虚拟键盘NavigationBar), 即屏幕高度-键盘高度(keyboardHeight)
*/
@Override
public void onChange(boolean isShow, int keyboardHeight, int screenWidth, int screenHeight) {
// do sth.
}
}));

三、更新日志
四、致谢
感谢facebook/react-native提供代码解决方案。
本仓库使用的react-native相关代码:
-
ReactRootView.java里的内部类CustomGlobalLayoutListener
监听键盘事件的监听器。
-
通过反射获取相关尺寸数值。
-
PixelUtil.java(非必要)
度量单位转换工具。px<->sp/dp之间的转化。
Related Skills
node-connect
345.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
106.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
