如何捕捉“虚拟键盘显示/隐藏"Android中的事件? [英] How to capture the "virtual keyboard show/hide" event in Android?

查看:36
本文介绍了如何捕捉“虚拟键盘显示/隐藏"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 更新

现在可以了:

在 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 preview 和相应的文档

Android 11 之前

然而,这项工作尚未在 Compat 版本中提供,因此您需要求助于 hacks.

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.或者,这里的页面提供了一些不同的方法来实现此 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/topic/resources/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中的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆