Hide the keyboard

5

As soon as the user clicks on one of the EditText of my Android application, the keyboard appears, however, it does not disappear when it finishes typing and clicks off of it.

I would like to know which method would be appropriate, knowing that I have 3 EditText .

I have tried to use all of the following methods, but none have successfully:

link

Edit .: The class code is here .

The line of code corresponding to the error after the change is:

editRed.setOnFocusChangeListener(new OnFocusChangeListener()
    
asked by anonymous 14.05.2014 / 16:20

2 answers

11

I believe clicking outside of EditText will not make the virtual keyboard disappear. You need to implement a listener that hides the keyboard when you click outside it (that is, when EditText loses focus). For example:

    searchEditText.setOnFocusChangeListener(new OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus)
        {
            if (false == hasFocus) {
                ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                        searchEditText.getWindowToken(), 0);
            }
        }
    });
    
14.05.2014 / 16:34
1

Utility method I use to do this:

public static void hideKeyboard(Context context, View editText) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
    
14.05.2014 / 17:07