监听虚拟键盘弹出的高度

Posted rainboy2010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听虚拟键盘弹出的高度相关的知识,希望对你有一定的参考价值。

原理

监听DecorView的可见高度,当虚拟键盘弹出的时候,DecorView的可见高度会变小,这时拿android.R.id.content控件的高度-可见矩形的bottom得到的就是虚拟键盘的高度,代码如下:

public interface KeyboardListener {
    void onKeyboardChange(boolean isPopup, int keyboardHeight);
}
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener {

    private KeyboardListener listener;
    private int mTempKeyboardHeight;
    private int navigationBarHeight;
    private Activity activity;
    private View decorView;

    public KeyboardUtils(Activity activity,KeyboardListener listener){
        this.activity = activity;
        this.listener = listener;
        decorView = activity.getWindow().getDecorView();
        navigationBarHeight=getNavigationBarHeight(activity);

    }

    public void enable(){
        if(activity!=null) {
            activity.getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
    }

    public void disable(){
        if (activity != null) {
            activity.getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    }


    @Override
    public void onGlobalLayout() {
        if (activity != null) {
            Rect r = new Rect();
            FrameLayout contentView = decorView.findViewById(android.R.id.content);
            decorView.getWindowVisibleDisplayFrame(r);
            int contentHeight = contentView.getHeight();
            int keyboardHeight = contentHeight-r.bottom;
            if (keyboardHeight != mTempKeyboardHeight) {
                mTempKeyboardHeight=keyboardHeight;
                boolean isPopup=false;
                if (keyboardHeight > navigationBarHeight) {
                    isPopup = true;
                }
                if (keyboardHeight < 0) {
                    keyboardHeight = 0;
                }
                if (listener != null) {
                    listener.onKeyboardChange(isPopup, keyboardHeight);
                }
            }
        }
    }
    
    private int getNavigationBarHeight(Context context){
        int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId>0) {
            return context.getResources().getDimensionPixelSize(resourceId);
        } else {
            return 0;
        }
    }
}

使用

在Activity中添加以下代码:

  KeyboardUtils keyboardUtils=new KeyboardUtils(this, (isPopup, keyboardHeight) -> {
            Log.d("KeyboardUtils", "onKeyboardChange() called with: isPopup = [" + isPopup + "], keyboardHeight = [" + keyboardHeight + "]");
        });
        keyboardUtils.enable();

 

以上是关于监听虚拟键盘弹出的高度的主要内容,如果未能解决你的问题,请参考以下文章

Android软键盘弹出和收回监听

移动web 怎么捕获虚拟键盘弹出和关闭事件

android studio 点击布局文件隐藏弹出的键盘。

如何在Android中判断软键盘是不是弹出或隐藏

vue解决安卓手机软键盘弹出,页面高度被顶起

iOS键盘监听以及获取键盘高度