java onConfigurationChanged() 没有为 keyboardHidden 触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6279771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onConfigurationChanged() not firing for keyboardHidden
提问by Tony Lukasavage
I've followed the various questions and answers here to setup my Android activity to override the onConfigurationChanged()
in order to execute logic when the soft keyboard opens and closes. Here's the relevant excerpts from my code. I've boiled it down to the simplest scenario:
我已经按照这里的各种问题和答案来设置我的 Android 活动来覆盖onConfigurationChanged()
,以便在软键盘打开和关闭时执行逻辑。这是我的代码的相关摘录。我把它归结为最简单的场景:
AndroidManifest.xml
AndroidManifest.xml
...
<activity
android:name=".SearchActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation"
/>
...
SearchActivity.java
搜索活动.java
...
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this, "onConfigurationChanged()", Toast.LENGTH_SHORT).show();
}
...
The code above will display the Toast when I change orientation, but does nothing when the soft keyboard opens or closes. I have tested opening the soft keyboard via EditText focusing and by manually opening it with a long press on the menu button. Neither fire the onConfigurationChanged()
call.
当我改变方向时,上面的代码将显示 Toast,但在软键盘打开或关闭时什么也不做。我已经测试过通过 EditText 聚焦打开软键盘,并通过长按菜单按钮手动打开它。既不触发onConfigurationChanged()
呼叫。
So the code in place appears to work since orientation change fires, but I get nothing for the soft keyboard. Any ideas? If the answer is "onConfigurationChanged() doesn't catch soft keyboard events", what is an appropriate solution for detecting and handling this event?
因此,自方向更改触发以来,适当的代码似乎可以工作,但我对软键盘一无所获。有任何想法吗?如果答案是“onConfigurationChanged() 不捕获软键盘事件”,那么检测和处理此事件的合适解决方案是什么?
Just in case it's relevant, I am testing on a Droid X running Gingerbread.
以防万一,我正在运行 Gingerbread 的 Droid X 上进行测试。
采纳答案by Shawn Lauzon
No, onConfigurationChange() doesn't catch soft keyboard events: it's not a configuration change. The orientation change causes a new set of resources to be used (such as layout-land vs layout-port), which is the definition of a configuration change.
不, onConfigurationChange() 不会捕获软键盘事件:它不是配置更改。方向更改会导致使用一组新的资源(例如 layout-land 与 layout-port),这是配置更改的定义。
So how to do it? Well, There's no event fired when the keyboard is shown, but you can detect when the keyboard causes your layout to be adjusted.
那么怎么做呢?好吧,显示键盘时不会触发任何事件,但是您可以检测键盘何时导致您的布局被调整。
See How to check visibility of software keyboard in Android?for the code.
请参阅如何检查 Android 中软件键盘的可见性?对于代码。