在Android應用的開發中,有一些需求須要咱們獲取到輸入法的高度,可是官方的API並無提供相似的方法,因此咱們須要本身來實現。java
查閱了網上不少資料,試過之後都不理想。android
好比有的方法經過監聽佈局的變化來計算輸入法的高度,這種方式在Activity的配置中配置爲"android:windowSoftInputMode="adjustResize""時沒有問題,能夠正確獲取輸入法的高度,由於佈局此時確實會動態的調整。git
可是當Activity配置爲"android:windowSoftInputMode="adjustNothing""時,佈局不會在輸入法彈出時進行調整,上面的方式就會撲街。github
不過通過一番探索和測試,終於發現了一種方式能夠在即便設置爲adjustNothing時也能夠正確計算高度放方法。微信
同時也感謝這位外國朋友:
GitHub地址app
其實也就兩個類,我也作了一些修改,解決了一些問題,這裏也貼出來:ide
/** * The observer that will be notified when the height of * the keyboard has changed */ public interface KeyboardHeightObserver { /** * 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 */ void onKeyboardHeightChanged(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. */ public class KeyboardHeightProvider extends PopupWindow { /** The tag for logging purposes */ private final static String TAG = "sample_KeyboardHeightProvider"; /** The keyboard height observer */ private KeyboardHeightObserver observer; /** The cached landscape height of the keyboard */ private int keyboardLandscapeHeight; /** The cached portrait height of the keyboard */ private int 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 */ public KeyboardHeightProvider(Activity activity) { super(activity); this.activity = activity; LayoutInflater inflator = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); this.popupView = inflator.inflate(R.layout.keyboard_popup_window, null, false); setContentView(popupView); setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE | LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); parentView = activity.findViewById(android.R.id.content); setWidth(0); setHeight(LayoutParams.MATCH_PARENT); popupView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (popupView != null) { handleOnGlobalLayout(); } } }); } /** * 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. */ public void start() { if (!isShowing() && parentView.getWindowToken() != null) { setBackgroundDrawable(new ColorDrawable(0)); showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0); } } /** * Close the keyboard height provider, * this provider will not be used anymore. */ public void close() { 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. */ public void setKeyboardHeightObserver(KeyboardHeightObserver observer) { this.observer = observer; } /** * Get the screen orientation * * @return the screen orientation */ private int getScreenOrientation() { 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. */ private void handleOnGlobalLayout() { 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); } else if (orientation == Configuration.ORIENTATION_PORTRAIT) { this.keyboardPortraitHeight = keyboardHeight; notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation); } else { this.keyboardLandscapeHeight = keyboardHeight; notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation); } } private void notifyKeyboardHeightChanged(int height, int orientation) { if (observer != null) { observer.onKeyboardHeightChanged(height, orientation); } } }
此處以在Activity中的使用進行舉例。佈局
引入這兩個類後,在當前Activity中實現接口KeyboardHeightObserver:post
@Override public void onKeyboardHeightChanged(int height, int orientation) { String or = orientation == Configuration.ORIENTATION_PORTRAIT ? "portrait" : "landscape"; Logger.d(TAG, "onKeyboardHeightChanged in pixels: " + height + " " + or); }
在當前Activity定義成員變量,並在onCreate()中進行初始化測試
private KeyboardHeightProvider mKeyboardHeightProvider; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { ... mKeyboardHeightProvider = new KeyboardHeightProvider(this); new Handler().post(() -> mKeyboardHeightProvider.start()); }
初始化完成後,咱們要在Activity中的生命週期中也要進行處理,以避免內存泄露。
@Override protected void onResume() { super.onResume(); mKeyboardHeightProvider.setKeyboardHeightObserver(this); } @Override protected void onPause() { super.onPause(); mKeyboardHeightProvider.setKeyboardHeightObserver(null); } @Override protected void onDestroy() { super.onDestroy(); mKeyboardHeightProvider.close(); }
此時咱們就能夠正確獲取的當前輸入法的高度了,即便android:windowSoftInputMode="adjustNothing"時也能夠正確獲取到,這正是這個方法的強大之處,利用這個方法能夠實現好比相似微信聊天的界面,流暢切換輸入框,表情框等。
若有更多疑問,請參考個人其它Android相關博客:個人博客地址
做者:ExampleCode 連接:https://www.jianshu.com/p/6ab9c0507705 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。