How to check visibility of software keyboard in Android?

前端 未结 30 5049
半阙折子戏
半阙折子戏 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条回答
  •  萌比男神i
    2020-11-21 05:32

    My answer is basically the same as Kachi's answer, but I wrapped it into a nice helper class to clean up the way it's used throughout my app.

    import android.app.Activity;
    import android.app.Fragment;
    import android.graphics.Rect;
    import android.view.View;
    import android.view.ViewTreeObserver.OnGlobalLayoutListener;
    
    /**
     * Detects Keyboard Status changes and fires events only once for each change
     */
    public class KeyboardStatusDetector {
        KeyboardVisibilityListener visibilityListener;
    
        boolean keyboardVisible = false;
    
        public void registerFragment(Fragment f) {
            registerView(f.getView());
        }
    
        public void registerActivity(Activity a) {
            registerView(a.getWindow().getDecorView().findViewById(android.R.id.content));
        }
    
        public KeyboardStatusDetector registerView(final View v) {
            v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    v.getWindowVisibleDisplayFrame(r);
    
                    int heightDiff = v.getRootView().getHeight() - (r.bottom - r.top);
                    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                        /** Check this variable to debounce layout events */
                        if(!keyboardVisible) {
                            keyboardVisible = true;
                            if(visibilityListener != null) visibilityListener.onVisibilityChanged(true);
                        }
                    } else {
                        if(keyboardVisible) {
                            keyboardVisible = false;
                            if(visibilityListener != null) visibilityListener.onVisibilityChanged(false);
                        }
                    }
                }
            });
    
            return this;
        }
    
        public KeyboardStatusDetector setVisibilityListener(KeyboardVisibilityListener listener) {
            visibilityListener = listener;
            return this;
        }
    
        public static interface KeyboardVisibilityListener {
            public void onVisibilityChanged(boolean keyboardVisible);
        }
    }
    

    You can use this to detect keyboard changes anywhere throughout the app like this:

        new KeyboardStatusDetector()
                .registerFragment(fragment)  //register to a fragment 
                .registerActivity(activity)  //or register to an activity
                .registerView(view)          //or register to a view
                .setVisibilityListener(new KeyboardVisibilityListener() {
                    @Override
                    public void onVisibilityChanged(boolean keyboardVisible) {
                        if(keyboardVisible) {
                           //Do stuff for keyboard visible
                        }else {
                           //Do stuff for keyboard hidden
                        }
                    }
                });
    

    Note: only use one of the "register" calls. They all work the same and are only there for convenience

提交回复
热议问题