EditText lose focus only when I leave the screen

懵懂的女人 提交于 2019-12-13 05:19:09

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/23963093/edittext-lose-focus-only-when-i-leave-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!