Android 打开关闭键盘以及监听的工具类

public class KeyboardUtil {

    /**
     * 展示输入法软键盘
     *
     * @param activity
     * @param currentFocusedView 当前获得焦点了的view
     */
    public static void showInputKeyboard(Activity activity, View currentFocusedView) {
        if (activity == null) {
            return;
        }
        InputMethodManager imm = (InputMethodManager) activity.
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(currentFocusedView, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 隐藏输入法键盘
     *
     * @param activity
     */
    public static void closeInputKeyboard(Activity activity) {
        if (activity == null) {
            return;
        }
        InputMethodManager inputMethodManager = (InputMethodManager) activity.
                getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken()
                , InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private KeyboardUtil() {
    }

    private static KeyboardUtil sKeyboardUtil = new KeyboardUtil();

    public static KeyboardUtil getInstance() {
        return sKeyboardUtil;
    }

    private int lastheight = 0;

    public void setListener(Activity activity, OnKeyboardListener listener) {
        setOnKeyboardListener(listener);
        //拿到页面的共同布局
        final View decorView = activity.getWindow().getDecorView();
        //设置最底层布局的变化监听
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //int measuredHeight = decorView.getMeasuredHeight();//拿尺寸不好用
                Rect rect = new Rect();
                //拿这个控件在屏幕上的可见区域
                decorView.getWindowVisibleDisplayFrame(rect);
                int height = rect.height();
                //第一次刚进来的时候,给上一次的可见高度赋一个初始值,
                // 然后不需要再做什么比较了,直接return即可
                if (lastheight == 0) {
                    lastheight = height;
                    return;
                }
                //当前这一次的可见高度比上一次的可见高度要小(有比较大的高度差,大于300),
                // 认为是软键盘弹出
                if (lastheight - height > 300) {
                    //隐藏这个RoomFragment中的控件
                    if (mOnKeyboardListener != null) {
                        mOnKeyboardListener.onKeyboardShow(lastheight - height);
                    }
                }
                //当前这一次的可见高度比上一次的可见高度要大,认为是软键盘收缩
                if (height - lastheight > 300) {
                    if (mOnKeyboardListener != null) {
                        mOnKeyboardListener.onKeyboardHide(height - lastheight);
                    }
                }
                //记录下来
                lastheight = height;
            }
        });
    }


    private OnKeyboardListener mOnKeyboardListener;

    public void setOnKeyboardListener(OnKeyboardListener keyboardListener) {
        mOnKeyboardListener = keyboardListener;
    }

    public interface OnKeyboardListener {
        void onKeyboardShow(int i);

        void onKeyboardHide(int i);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值