0

I am trying to close the keyboard when the focus of EditText has been lost. And I use this code for that

edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {

            if (!hasFocus) {
                InputMethodManager imm = (InputMethodManager) getBaseContext().getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                Log.d(TAG, "edt focus lost");
            }

            if (hasFocus)
                Log.d(TAG, "edt has focus");
        }
    });

When I touch it, I immediately get the focus. But I don't lose focus until I leave the screen.

I tried opening other Dialogs on the page, touching other Views with their own listeners, but "focus lost" message is not fired before I leave the screen.

Why is this happening?

sandalone
  • 41,141
  • 63
  • 222
  • 338

1 Answers1

2

A focusable view (such as this EditText) only loses focus when another focusable view gains it. Most views are not focusable by default.

matiash
  • 54,791
  • 16
  • 125
  • 154