/** * The observer that will be notified when the height of * the keyboard has changed */ publicinterfaceKeyboardHeightObserver{
/** * Called when the keyboard height has changed, 0 means keyboard is closed, * >= 1 means keyboard is opened. * * @param height The height of the keyboard in pixels * @param orientation The orientation either: Configuration.ORIENTATION_PORTRAIT or * Configuration.ORIENTATION_LANDSCAPE */ voidonKeyboardHeightChanged(int height, int orientation); }
/** * The keyboard height provider, this class uses a PopupWindow * to calculate the window height when the floating keyboard is opened and closed. */ publicclassKeyboardHeightProviderextendsPopupWindow{
/** The tag for logging purposes */ privatefinalstatic String TAG = "sample_KeyboardHeightProvider";
/** The keyboard height observer */ private KeyboardHeightObserver observer;
/** The cached landscape height of the keyboard */ privateint keyboardLandscapeHeight;
/** The cached portrait height of the keyboard */ privateint keyboardPortraitHeight;
/** The view that is used to calculate the keyboard height */ private View popupView;
/** The parent view */ private View parentView;
/** The root activity that uses this KeyboardHeightProvider */ private Activity activity;
/** * Construct a new KeyboardHeightProvider * * @param activity The parent activity */ publicKeyboardHeightProvider(Activity activity){ super(activity); this.activity = activity;
/** * Start the KeyboardHeightProvider, this must be called after the onResume of the Activity. * PopupWindows are not allowed to be registered before the onResume has finished * of the Activity. */ publicvoidstart(){
/** * Close the keyboard height provider, * this provider will not be used anymore. */ publicvoidclose(){ this.observer = null; dismiss(); }
/** * Set the keyboard height observer to this provider. The * observer will be notified when the keyboard height has changed. * For example when the keyboard is opened or closed. * * @param observer The observer to be added to this provider. */ publicvoidsetKeyboardHeightObserver(KeyboardHeightObserver observer){ this.observer = observer; } /** * Get the screen orientation * * @return the screen orientation */ privateintgetScreenOrientation(){ return activity.getResources().getConfiguration().orientation; }
/** * Popup window itself is as big as the window of the Activity. * The keyboard can then be calculated by extracting the popup view bottom * from the activity window height. */ privatevoidhandleOnGlobalLayout(){
Point screenSize = new Point(); activity.getWindowManager().getDefaultDisplay().getSize(screenSize);
Rect rect = new Rect(); popupView.getWindowVisibleDisplayFrame(rect);
// REMIND, you may like to change this using the fullscreen size of the phone // and also using the status bar and navigation bar heights of the phone to calculate // the keyboard height. But this worked fine on a Nexus. int orientation = getScreenOrientation(); int keyboardHeight = screenSize.y - rect.bottom; if (keyboardHeight == 0) { notifyKeyboardHeightChanged(0, orientation); } elseif (orientation == Configuration.ORIENTATION_PORTRAIT) { this.keyboardPortraitHeight = keyboardHeight; notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation); } else { this.keyboardLandscapeHeight = keyboardHeight; notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation); } }
privatevoidnotifyKeyboardHeightChanged(int height, int orientation){ if (observer != null) { observer.onKeyboardHeightChanged(height, orientation); } } }
使用方法
此处以在Activity中的使用进行举例。
实现接口
引入这两个类后,在当前Activity中实现接口KeyboardHeightObserver:
1 2 3 4 5
@Override publicvoidonKeyboardHeightChanged(int height, int orientation){ String or = orientation == Configuration.ORIENTATION_PORTRAIT ? "portrait" : "landscape"; Logger.d(TAG, "onKeyboardHeightChanged in pixels: " + height + " " + or); }