How to capture the quot;virtual keyboard show/hidequot; event in Android?(如何捕捉“虚拟键盘显示/隐藏?Android中的事件?)
问题描述
我想根据是否显示虚拟键盘来更改布局.我搜索了 API 和各种博客,但似乎找不到任何有用的东西.
I would like to alter the layout based on whether the virtual keyboard is shown or not. I've searched the API and various blogs but can't seem to find anything useful.
有可能吗?
谢谢!
推荐答案
2020年更新
这现在是可能的:
2020 Update
This is now possible:
在 Android 11 上,您可以这样做
On Android 11, you can do
view.setWindowInsetsAnimationCallback(object : WindowInsetsAnimation.Callback {
override fun onEnd(animation: WindowInsetsAnimation) {
super.onEnd(animation)
val showingKeyboard = view.rootWindowInsets.isVisible(WindowInsets.Type.ime())
// now use the boolean for something
}
})
还可以听键盘显示/隐藏的动画,做相应的过渡.
You can also listen to the animation of showing/hiding the keyboard and do a corresponding transition.
我推荐阅读Android 11 预览版 和相应的文档
Android 11 之前
但是,这项工作尚未在 Compat
版本中提供,因此您需要求助于 hack.
However, this work has not been made available in a Compat
version, so you need to resort to hacks.
您可以获得窗口插图,如果底部插图大于您认为相当好的某个值(通过实验),您可以认为这是显示键盘.这不是很好,在某些情况下可能会失败,但没有框架支持.
You can get the window insets and if the bottom insets are bigger than some value you find to be reasonably good (by experimentation), you can consider that to be showing the keyboard. This is not great and can fail in some cases, but there is no framework support for that.
对于这个确切的问题 https://stackoverflow.com/a/36259261/372076,这是一个很好的答案.或者,这里有一个页面提供了一些不同的方法来实现这个 pre Android 11:
This is a good answer on this exact question https://stackoverflow.com/a/36259261/372076. Alternatively, here's a page giving some different approaches to achieve this pre Android 11:
https://developer.salesforce.com/docs/atlas.en-us.noversion.service_sdk_android.meta/service_sdk_android/android_detecting_keyboard.htm
此解决方案不适用于软键盘和onConfigurationChanged
不会被调用软(虚拟)键盘.
This solution will not work for soft keyboards and
onConfigurationChanged
will not be called for soft (virtual) keyboards.
您必须自己处理配置更改.
You've got to handle configuration changes yourself.
http://developer.android.com/guide/主题/资源/runtime-changes.html#HandlingTheChange
示例:
// from the link above
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
然后只需更改某些视图的可见性、更新字段并更改布局文件.
Then just change the visibility of some views, update a field, and change your layout file.
这篇关于如何捕捉“虚拟键盘显示/隐藏"?Android中的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何捕捉“虚拟键盘显示/隐藏"?Android中的事件?
- android 4中的android RadioButton问题 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01