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

被刻印的时光 ゝ 提交于 2019-12-10 13:05:30

问题


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.



来源:https://stackoverflow.com/questions/5961927/how-to-know-if-keyboard-disappears-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!