Android 开发学习进程0.29 软键盘使用问题

软键盘使用问题

软键盘在edittext获取焦点后自动弹出,需要设置监听:

  etActsearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // close soft keyboard
                    InputMethodManager inputMethodManager = (InputMethodManager) v.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }

                    search(etActsearchInput.getText().toString());
                    return true;
                }
                return false;
            }
        });

以上实现了监听 软键盘的确定键,同时 edittext布局文件同样需设置

android:singleLine="true"
android:imeOptions="actionSearch"

imeOptions属性决定了软键盘右下角按键文字,属性包括搜索、确定、换行。同时由edit源码知:调用软键盘与关闭软键盘是相似的,需要分两步:
1、获取InputMethodManager 实例
2、调用showSoftInput(xx)
非在editview中使用软键盘时,如button设置Button.setFocusableInTouchMode(true)
设置弹出软键盘时activity布局是否被顶上去
getWindow().getAttributes().softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
或是在manifest文件中设置android:windowSoftInputMode="adjustResize"
部分app有 activity的window不往上移动,而editview移动至软键盘上,这样实现的原理是,需要设置属性
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
同时计算软键盘高度,移动editview坐标。点击外部隐藏软键盘:

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
              //获取焦点View
              View v = getCurrentFocus();
            if (isFocusEditText(v, hideSoftByEditViewIds())) {
                //如果焦点在editText上
                //隐藏键盘
                KeyBoardUtils.hideInputForce(this);
                //清除焦点
                clearViewFocus(v, hideSoftByEditViewIds());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

可以选择id过滤的函数:

 @Override
    public int[] hideSoftByEditViewIds() {
        int[] ids = {R.id.et_phone, R.id.et_check_code, R.id.et_city_code};
        return ids;
    }

最后,在是否判断软键盘打开时如果仅仅采用inputmethodmanager的isactive方法判断可能会出问题,这个方法返回的是edittext是否获得焦点,如果获得焦点的话就会一直返回true,应采用判断布局高度的方法:

  int screenHeight=getWindow().getDecorView().getHeight();
        Rect rect=new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        return screenHeight*2/3>rect.bottom;

判断可视窗口大于底距离大于屏幕2/3
部分情况可能需要判断软键盘高度,使用原窗口高度减去弹出键盘窗口高度

private int mWindowHeight = 0;
private ViewTreeObserver.OnGlobalLayoutListener mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Rect r = new Rect();
        //获取当前窗口实际的可见区域
        getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
        int height = r.height();
        if (mWindowHeight == 0) {
            //一般情况下,这是原始的窗口高度
            mWindowHeight = height;
        } else {
            if (mWindowHeight != height) {
                //两次窗口高度相减,就是软键盘高度
                int softKeyboardHeight = mWindowHeight - height;
                System.out.println("SoftKeyboard height = " + softKeyboardHeight);
            }
        }
    }
};

在oncreate生命周期添加监听 getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);
同时需要在destroy后移除监听 getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener);

吾生也有涯,而知也無涯。以有涯隨無涯,殆已
原文地址:https://www.cnblogs.com/baimiyishu/p/14484814.html