软键盘相关知识

一,点击按钮,控制软键盘,如果是打开的则关闭,如果是关闭的则打开

private void hideOrOpenKeyBoard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isActive()) {
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}


二,Activity中有一个editText,若刚进入时,不想软键盘弹出,只想在输入时,才让软键盘弹出,则在oncreate()中设置

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//隐藏软键盘


三,软键盘和Edittext的imeOptions属性

<EditText
    android:imeOptions="actionGo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

(1)actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED

(2)actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE

(3)actionGo去往,对应常量EditorInfo.IME_ACTION_GO 

(4)actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH

(5)actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND

(6)actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT

(7)actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE


四,动态计算软键盘的高度,并动态的设置需要覆盖的布局的高度

扫描二维码关注公众号,回复: 2515740 查看本文章

private void getSoftHeight( ) {
    SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {


        @Override
        public void keyBoardShow(int height) {
            Log.e("date", "打开的软键盘的高度: "+height );
            ViewGroup.LayoutParams params = mRl_picture.getLayoutParams();
            params.height = height;
            mRl_picture.setLayoutParams(params);
        }

        @Override
        public void keyBoardHide(int height) {
            Log.e("date", "隐藏的软键盘的高度: "+height );
            ViewGroup.LayoutParams params = mRl_picture.getLayoutParams();
            params.height = height;
            mRl_picture.setLayoutParams(params);
        }
    });
}



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();
                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);
    }
}




猜你喜欢

转载自blog.csdn.net/qq_38859786/article/details/81027888