Click outside of EditText, the soft keyboard is hidden

In Android programming, we often need to click EditText, the soft keyboard pops up, with external area clicked, and the soft keyboard is hidden. Here are two ways I can provide:
1. We set the click event for the outermost ViewGroup of the layout, and then click to hide the soft keyboard. Isn't it very simple, but this way can only solve the simple situation of the layout. For the complicated situation, please look at the second way directly. The code is as follows:

In layout:

<?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="Please enter a personal signature within 30 words"
        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>

In the code:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_introduce);
        ButterKnife.bind(this); //Here I use a butter knife, findViewById() and setOnClickListener()
    }

    @OnClick(R.id.ll_edit_introduce)
    void clickHideKeyboard() { //Click event for outer layout
        hideKeyboard(mUserIntroduce); //Call the method to hide the keyboard here
    }
    /**
     * Hide keyboard
     */
    protected void hideKeyboard(View v) {
        ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(v.getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
    }

2. The second method can solve most of the requirements. The code is as follows:

    /**
     * Solve the problem of clicking EditText, clicking the external area, soft keyboard hiding
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) { //Need to hide soft keyboard

                InputMethodManager imm = (InputMethodManager)     getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                mEditText.setCursorVisible(false);
            }
            return super.dispatchTouchEvent(ev);
        }
        // Necessary, otherwise all components will not have TouchEvent
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    /**
     * Judge whether the current click position is EditText
     *
     * @param v
     * @param event
     * @return
     */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) { //If the view you click is EditText
            int[] leftTop = {0, 0};
            //Get the current location of the input box
            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) {
                // Click the input box area to keep the event of clicking EditText
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

In this way, we can solve the need of hiding the soft keyboard by clicking the outside of EditText

Tags: Android Programming xml encoding

Posted on Mon, 25 May 2020 11:13:02 -0400 by lawnmowerman