點擊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外部,軟鍵盤隱藏的需求了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章