How to check visibility of software keyboard in Android?

前端 未结 30 4489
半阙折子戏
半阙折子戏 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:21

    I used a slight variant of Reuban's answer, which proved to be more helpful in certain circumstances, especially with high resolution devices.

    final View activityRootView = findViewById(android.R.id.content);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightView = activityRootView.getHeight();
                    int widthView = activityRootView.getWidth();
                    if (1.0 * widthView / heightView > 3) {
                        //Make changes for Keyboard not visible
                    } else {
                        //Make changes for keyboard visible
                    }
                }
            });
    

提交回复
热议问题