Android: How to unfocus a SearchView programmatically

拜拜、爱过 提交于 2019-12-14 03:54:26

问题


I have a SearchView in my layout (not in the action bar) and I'm unable to dismiss the keyboard using the usual method:

public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager)activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm != null && activity != null) {
        View currentFocus = activity.getCurrentFocus();

        if (currentFocus != null) {
            imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
        }
    }
}

The SearchView maintains its focus state. Using this method on older devices shows the keyboard being dismissed then redisplaying.

I believe the issue is with the fact that a SearchView is actually a hierarchy of Views that maintain their own state internally.

How do I dismiss the keyboard and unfocus the SearchView?


回答1:


It sounds like what might be happening is that you're unfocusing the SearchView and then it's saying "oh wait, I still have focus, I need the keyboard". Does activity.getCurrentFocus().clearFocus() clear out the focus?



来源:https://stackoverflow.com/questions/17645799/android-how-to-unfocus-a-searchview-programmatically

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