软键盘相关

一、KeyboardChangeListener

public class KeyboardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
    private static final String TAG = "KeyboardChangeListener";
    public static final int MIN_KEYBOARD_HEIGHT = 300;
    private KeyboardListener mKeyboardListener;
    private boolean mShowFlag = false;
    private Window mWindow;
    private View mContentView;

    public interface KeyboardListener {
        /**
         * call back
         * @param isShow         true is show else hidden
         * @param keyboardHeight keyboard height
         */
        void onKeyboardChange(boolean isShow, int keyboardHeight);
    }

    public void setKeyboardListener(KeyboardListener keyboardListener) {
        this.mKeyboardListener = keyboardListener;
    }

    public static KeyboardChangeListener create(Activity activity){
        return new KeyboardChangeListener(activity);
    }

    public static KeyboardChangeListener create(Dialog dialog){
        return new KeyboardChangeListener(dialog);
    }

    private KeyboardChangeListener(Object contextObj) {
        if (contextObj == null) {
            Log.d(TAG, "contextObj is null");
            return;
        }
        if (contextObj instanceof Activity) {
            mContentView = findContentView((Activity) contextObj);
            mWindow = ((Activity) contextObj).getWindow();
        } else if (contextObj instanceof Dialog) {
            mContentView = findContentView((Dialog) contextObj);
            mWindow = ((Dialog) contextObj).getWindow();
        }
        if (mContentView != null && mWindow != null) {
            addContentTreeObserver();
        }

    }

    private View findContentView(Activity contextObj) {
        return contextObj.findViewById(android.R.id.content);
    }

    private View findContentView(Dialog contextObj) {
        return contextObj.findViewById(android.R.id.content);
    }

    private void addContentTreeObserver() {
        mContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }


    @Override
    public void onGlobalLayout() {
        if (mContentView == null || mWindow == null) {
            return;
        }
        int currentViewHeight = mContentView.getHeight();
        if (currentViewHeight == 0) {
            Log.d(TAG, "currHeight is 0");
            return;
        }
        int screenHeight = getScreenHeight();
        int windowBottom;
        int keyboardHeight;

        Rect rect = new Rect();
        mWindow.getDecorView().getWindowVisibleDisplayFrame(rect);
        windowBottom = rect.bottom;

        keyboardHeight = screenHeight - windowBottom;

        Log.d(TAG, "onGlobalLayout() called " + " screenHeight " + screenHeight + " VisibleDisplayHeight " + windowBottom);

        if (mKeyboardListener != null) {
            boolean currentShow = keyboardHeight > MIN_KEYBOARD_HEIGHT;
            if (mShowFlag != currentShow) {
                mShowFlag = currentShow;
                mKeyboardListener.onKeyboardChange(currentShow, keyboardHeight);
            }
        }
    }

    private int getScreenHeight() {
        Display defaultDisplay = mWindow.getWindowManager().getDefaultDisplay();
        int screenHeight = 0;
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            defaultDisplay.getRealSize(point);
        } else {
            defaultDisplay.getSize(point);
        }
        screenHeight = point.y;
        return screenHeight;
    }


    public void destroy() {
        if (mContentView != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mContentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    }
}

使用


       KeyboardChangeListener.create(FristSearchAct.this).setKeyboardListener(new KeyboardChangeListener.KeyboardListener() {
            @Override
            public void onKeyboardChange(boolean isShow, int keyboardHeight) {
                if (isShow){
                    
                }else {
                   
                }
                Log.d(TAG, "isShow = [" + isShow + "], keyboardHeight = [" + keyboardHeight + "]");
            }
        });
  

简单判断是否显示

implementation 'com.blankj:utilcodex:1.30.6'//非常实用得工具类,强烈推荐


 if (KeyboardUtils.isSoftInputVisible(NewMainActivity.this)){
      
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值