Recently in the project, you need to get the height of the soft keyboard, and then move the bottom layout up the height of the keyboard, without saying a word, directly on the code:

Gets the soft keyboard height

Public static int keyboardHeight = 0; boolean isVisiableForLast =false;
    ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = null;

    public void addOnSoftKeyBoardVisibleListener() {
        if (keyboardHeight > 0) {
            return;
        }
        final View decorView = getWindow().getDecorView();
        onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() { Rect rect = new Rect(); decorView.getWindowVisibleDisplayFrame(rect); Int displayHight = rect.bottom-rect.top; int displayHight = rect.bottom-rect.top; Int hight = decorView.getheight (); Boolean visible = (double) displayHight/hight < 0.8; int statusBarHeight = 0; try { Class<? > c = Class.forName("com.android.internal.R$dimen");
                    Object obj = c.newInstance();
                    Field field = c.getField("status_bar_height");
                    int x = Integer.parseInt(field.get(obj).toString());
                    statusBarHeight = getApplicationContext().getResources().getDimensionPixelSize(x);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if(visible && visible ! = isVisiableForLast) {// get keyboardHeight keyboardHeight = hight - displayHight - statusBarHeight; Log.i("keyboardHeight==1213==".""+ keyboardHeight); } isVisiableForLast = visible; }}; decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener); }Copy the code

Keyboard open and close operations:

Public class KeybordS {/** * open the keyboard */ public static void openKeybord(EditText mEditText, Context mContext) { mEditText.setFocusable(true);
        mEditText.setFocusableInTouchMode(true); mEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } public static void closeKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); } /** * public static void hideInput(Activity Activity) {if(activity.getCurrentFocus() ! = null) { InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); }} public static Boolean isSoftInputShow(Activity Activity) {public static Boolean isSoftInputShow(Activity Activity) activity.getWindow().peekDecorView();if(view ! InputMethodManager InputManger = (InputMethodManager) Activity .getSystemService(Activity.INPUT_METHOD_SERVICE); // inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);returninputmanger.isActive() && activity.getWindow().getCurrentFocus() ! = null; }return false; }}Copy the code

Monitor whether the keyboard is open or closed:

private void setListenerToRootView() { final View rootView = getWindow().getDecorView().findViewById(android.R.id.content); rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { boolean mKeyboardUp = isKeyboardShown(rootView); If (mKeyboardUp) {// keyboard popup // toast.makeText (getApplicationContext(), "keyboard popup ", toast.length_short).show(); Toast.maketext (getApplicationContext(), "toast.length_short ", toast.length_short).show(); }}}); } private boolean isKeyboardShown(View rootView) { final int softKeyboardHeight = 100; Rect r = new Rect(); rootView.getWindowVisibleDisplayFrame(r); DisplayMetrics dm = rootView.getResources().getDisplayMetrics(); int heightDiff = rootView.getBottom() - r.bottom; return heightDiff > softKeyboardHeight * dm.density; }Copy the code

The following is our personal public account (LongXuanzhigu). Our articles will be synchronized to this account, which is convenient for exchanging and learning Android knowledge and sharing personal favorite articles: