How to check visibility of software keyboard in Android?

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

    Sorry for the late answer, but I had created a little helper class to handle open/close events with notifying listeners and other useful things, may be someone would find it helpful:

    import android.graphics.Rect;
    import android.view.View;
    import android.view.ViewTreeObserver;
    
    import java.util.LinkedList;
    import java.util.List;
    
    public class SoftKeyboardStateWatcher implements ViewTreeObserver.OnGlobalLayoutListener {
    
        public interface SoftKeyboardStateListener {
            void onSoftKeyboardOpened(int keyboardHeightInPx);
            void onSoftKeyboardClosed();
        }
    
        private final List listeners = new LinkedList();
        private final View activityRootView;
        private int        lastSoftKeyboardHeightInPx;
        private boolean    isSoftKeyboardOpened;
    
        public SoftKeyboardStateWatcher(View activityRootView) {
            this(activityRootView, false);
        }
    
        public SoftKeyboardStateWatcher(View activityRootView, boolean isSoftKeyboardOpened) {
            this.activityRootView     = activityRootView;
            this.isSoftKeyboardOpened = isSoftKeyboardOpened;
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
    
        @Override
        public void onGlobalLayout() {
            final Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            activityRootView.getWindowVisibleDisplayFrame(r);
    
            final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
            if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                isSoftKeyboardOpened = true;
                notifyOnSoftKeyboardOpened(heightDiff);
            } else if (isSoftKeyboardOpened && heightDiff < 100) {
                isSoftKeyboardOpened = false;
                notifyOnSoftKeyboardClosed();
            }
        }
    
        public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
            this.isSoftKeyboardOpened = isSoftKeyboardOpened;
        }
    
        public boolean isSoftKeyboardOpened() {
            return isSoftKeyboardOpened;
        }
    
        /**
         * Default value is zero {@code 0}.
         *
         * @return last saved keyboard height in px
         */
        public int getLastSoftKeyboardHeightInPx() {
            return lastSoftKeyboardHeightInPx;
        }
    
        public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
            listeners.add(listener);
        }
    
        public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
            listeners.remove(listener);
        }
    
        private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
            this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
    
            for (SoftKeyboardStateListener listener : listeners) {
                if (listener != null) {
                    listener.onSoftKeyboardOpened(keyboardHeightInPx);
                }
            }
        }
    
        private void notifyOnSoftKeyboardClosed() {
            for (SoftKeyboardStateListener listener : listeners) {
                if (listener != null) {
                    listener.onSoftKeyboardClosed();
                }
            }
        }
    }
    

    Usage example:

    final SoftKeyboardStateWatcher softKeyboardStateWatcher 
        = new SoftKeyboardStateWatcher(findViewById(R.id.activity_main_layout);
    
    // Add listener
    softKeyboardStateWatcher.addSoftKeyboardStateListener(...);
    // then just handle callbacks
    

提交回复
热议问题