How to know if keyboard (dis)appears in Android?

2019-06-17 04:10发布

问题:

I have an EditText and want to give it more lines when the keyboard appears. So i am looking for something like a "OnKeyboardAppearsListener" but can't find it. I think it must exist, but perhaps in a different way...

回答1:

You have to @Override onConfigurationChanged to be able to handle runtime changes:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks whether a hardware or on-screen keyboard is available
    if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "Keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "Keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}

Example taken from here. Take a look here for keyboard related (among others) fields you might want to use.


Edit (RivieraKid): Changed to take account of hard or on-screen keyboard.