Android EditText, soft keyboard show/hide event?

前端 未结 7 1985
天涯浪人
天涯浪人 2020-11-29 03:43

Is it possible to catch the event that Soft Keyboard was shown or hidden for EditText?

7条回答
  •  生来不讨喜
    2020-11-29 04:29

    I solved this issue by using onGlobalLayoutListener :

     final View activityRootView = findViewById(R.id.top_root);
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
    
                    if (heightDiff > 100) {
                        // keyboard is up
                    } else {
                        // keyboard is down
                    }
                }
            });
    

    Here activityRootView is your Activity's root view.

提交回复
热议问题