点击EditText外部隐藏软键盘的小技巧

在Android编程中,我们经常需要实现点击EditText,软键盘弹起,带点击外部区域,软键盘隐藏,下面我提供两种方式:
1、我们给布局的最外层ViewGroup设置点击事件,点击就隐藏软键盘,是不是很简单,但是这种方式只能解决布局简单的情况,复杂的请直接看第二种,代码如下:

布局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/ll_edit_introduce">

    <EditText
        android:id="@+id/user_introduce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入30字以内的个性签名"
        android:textColorHint="@color/common_hint"
        android:textColor="@color/title"
        android:maxLength="30"
        android:paddingStart="@dimen/common_margin"
        android:paddingEnd="@dimen/common_margin"
        android:paddingTop="@dimen/common_margin"
        android:paddingBottom="@dimen/common_margin"
        android:textSize="14sp"
        android:background="@color/white"
        android:cursorVisible="false"
        android:textCursorDrawable="@drawable/shape_cursor_color"
        />

</LinearLayout>

代码中:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_introduce);
        ButterKnife.bind(this); //这里我用了黄油刀,findViewById()然后setOnClickListener()也可以
    }

    @OnClick(R.id.ll_edit_introduce)
    void clickHideKeyboard() { //外层布局的点击事件
        hideKeyboard(mUserIntroduce); //在此调用隐藏键盘的方法
    }
    /**
     * 隐藏键盘
     */
    protected void hideKeyboard(View v) {
        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(v.getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
    }

2、第二种方法可以解决大部分需求,代码如下:

    /**
     * 解决点击EditText点击外部区域软键盘隐藏
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) { //需要隐藏软键盘

                InputMethodManager imm = (InputMethodManager)     getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                mEditText.setCursorVisible(false);
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少,否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    /**
     * 判断当前点击的位置是否为EditText
     *
     * @param v
     * @param event
     * @return
     */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) { //如果点击的view是EditText
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域,保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

这样,我们就可以解决点击EditText外部,软键盘隐藏的需求了

猜你喜欢

转载自blog.csdn.net/k_bb_666/article/details/78805971