How to check visibility of software keyboard in Android?

前端 未结 30 4430
半阙折子戏
半阙折子戏 2020-11-21 04:43

I need to do a very simple thing - find out if the software keyboard is shown. Is this possible in Android?

30条回答
  •  遥遥无期
    2020-11-21 05:35

    I know that this is a old post but I think this is the simplest approach that I know and my test device is Nexus 5. I haven't tried it in other devices. Hope that others will share their approach if they find my code is not good :)

    public static boolean isKeyboardShown(Context context, View view) {
            if (context == null || view == null) {
                return false;
            }
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    }
    

    imm.hideSoftInputFromWindow returns boolean.

    Thanks,

提交回复
热议问题