How do I detect if software keyboard is visible on Android Device or not?

前端 未结 30 1383
情书的邮戳
情书的邮戳 2020-11-22 10:59

Is there a way in Android to detect if the software (a.k.a. \"soft\") keyboard is visible on screen?

30条回答
  •  粉色の甜心
    2020-11-22 11:45

    I did this by setting a GlobalLayoutListener, as follows:

    final View activityRootView = findViewById(R.id.activityRoot);
    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
                    }
                }
            });
    

提交回复
热议问题