开发者

onConfigurationChanged() not firing for keyboardHidden

开发者 https://www.devze.com 2023-03-11 02:02 出处:网络
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 th

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:

AndroidManifest.xml

...
<activity 
    android:name=".SearchActivity" 
    android:label="@string/app_name" 
    android:configChanges="keyboard|keyboardHidden|orientation"
/>
...

SearchActivity.java

...
@Override
public开发者_C百科 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.

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?

Just in case it's relevant, I am testing on a Droid X running Gingerbread.


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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消