SoFunction
Updated on 2025-04-09

Analysis and solutions for the reasons why the EditText+Button combination in Android causes the input board to fail to be closed

In Android development, entering information is the most basic operation and is widely used.

However, Android's support for input method pop-up/collapse is not very good.

For pop-up, the force method and implicit method are provided, but the force method is not provided for the input.

It is conceivable how painful it is to be able to be retracted if you want to be retracted if you want to be retracted!

If no processing is done on the input method, clicking Button will automatically close the input method after EditText is entered.

If you haven't closed it, it may be that there are some problems with the layout. You can try adding a scrollView to the outermost layer.

The author personally tests that in most cases, nested scrollView is feasible.

If the above method still cannot solve the problem and is obsessed with the automatic folding of the input board (unfortunately, PMs are generally so obsessed), you can consider the following method

Modification to EditText

Rewrite the onFocusChange method of EditText and add the following code

InputMethodManager manager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (!hasFocus) {
(getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} else {
(this, );
} 

This code ensures that EditText pops up the input method when it gets focus and can turn off the input method when it loses focus.

Modification to Button

Call the following method in the onClick method and perform business processing after the call

public static void obtainFocus(View v) {
(true);
();
(false);
}

Through the above two-end codes, the input board can be closed normally after clicking Button.

However, if you enter the page and let the input board pop up, this needs to be processed separately.

principle

EditText section

By monitoring the changes in focus, display and hide the input board.

The focus changes can be monitored through the method.

Button part

After clicking Button, first get the focus and then perform business processing.

Click on the event to register.

You may have questions about the call to setFocusableInTouchMode twice. In fact, the call is to be able to perform multiple requestFocus method (the requestFocus method requires focusableInTouchMode to be true).

When FocusableInTouchMode is true, clicking Button for the first time will make Button gain focus, and clicking again will call back onClick.

In order to ensure that onClick can be called back every click, we first set focusableInTouchMode to true, so that requestFocus can be called for focus acquisition, and then set focusableInTouchMode to false to ensure that onClick can still be corresponding normally when clicking next time.