【Android】软键盘弹出收起事件监听

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sko121/article/details/82387507

在开发过程中有时候会有监听软键盘弹出收起事件的需求,在此作记录,以便以后再次遇到,少走弯路。

弹出和隐藏软键盘方法:

 /**
     * 弹出软键盘
     */
    public void showKeyBoard(View v) {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(null != imm) {
            v.requestFocus();
            imm.showSoftInput(v, 0);
        }
        isKeyboardShow = true;
    }

 /**
     * 隐藏软键盘
     */
    public void hideKeyBoard(View v) {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(null != imm) {
            v.requestFocus();
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        isKeyboardShow = false;
    }

基于以上的弹出收起操作,给出监听实体类:

/**
 * Created by Luzj on 2018/8/20.
 *
 * 软键盘弹出收起监听
 */
public class SoftKeyBoardListener {

    private View rootView;//activity的根视图
    int rootViewVisibleHeight;//纪录根视图的显示高度
    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;

    public SoftKeyBoardListener(Activity activity) {
        //获取activity的根视图
        rootView = activity.getWindow().getDecorView();

        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获取当前根视图在屏幕上显示的大小
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);

                int visibleHeight = r.height();
                System.out.println(""+visibleHeight);
                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
                if (rootViewVisibleHeight == visibleHeight) {
                    return;
                }

                //根视图显示高度变小超过200,可以看作软键盘显示了
                if (rootViewVisibleHeight - visibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度变大超过200,可以看作软键盘隐藏了
                if (visibleHeight - rootViewVisibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

            }
        });
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }

    public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
        softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
    }
}

在Activity中注册监听:

SoftKeyBoardListener.setListener(this, onSoftKeyBoardChangeListener);

声明监听实例:

/**
     * 软键盘弹出收起监听
     */
    private SoftKeyBoardListener.OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener = new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
        @Override
        public void keyBoardShow(int height) {
            show("键盘显示 高度 : " + height);
        }
        @Override
        public void keyBoardHide(int height) {
            show("键盘隐藏 高度 : " + height);
        }
    };

 ps:

有时候弹出软键盘会使布局出现出乎预期的现象,这时候要考虑Manifest文件中Activity的windowSoftInputMode属性。

在我的开发中,想做软键盘弹出时,部分屏幕出现蒙层的结果,但是在软键盘弹出时蒙层总会自动消失,这时候在Manifest的相应Activity中加上:

android:windowSoftInputMode="adjustPan"

 结果正常。

总结:软键盘导致的布局问题,往往在windowSoftInputMode属性中可以找到答案。

猜你喜欢

转载自blog.csdn.net/sko121/article/details/82387507