SearchView.isFocused always returns false

2019-07-22 00:36发布

问题:

I'm trying to determine whether a SearchView in the ActionBar is focused. But when I call SearchView.isFocused() I always get false as a result, even when the view is really focused (there is a cursor inside, and the soft keyboard is shown).

How can I check, whether a SearchView is focused?

回答1:

After some researching, I learned that SearchView.isFocused() always returns false because it's some child of the SearchView who really has the focus, not the SearchView itself. So I use the following code to check the focus of a SearchView:

private boolean checkFocusRec(View view) {
    if (view.isFocused())
        return true;

    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            if (checkFocusRec(viewGroup.getChildAt(i)))
                return true;
        }
    }
    return false;
}

So I call checkFocusRec(searchView) to check for the focus. I'm not sure this is the optimal solution but it works for me.



回答2:

When looking at this question and its solution, I found some extra information in the resource files included with the Android SDK (in the folder sdk/platforms/android-xx/data/res/layout/search_view.xml).

The subview holding focus has id @+id/search_src_text and is of type android.widget.SearchView$SearchAutoComplete.

I would therefore suggest the following alternative to the previous solution:

private boolean isSearchViewFocused(SearchView sview) {
    View et_search =  sview.findViewById(R.id.search_src_text);
    return et_search.isFocused();
}


回答3:

Looks like an ugly (or bug) implementation from SearchView. My suggestion is to extend the SearchView and create methods like so:

var isQueryFocused = false
    private set

// you can also override hasFocus() and isFocused() methods, if you prefer
fun hasQueryFocus() = isQueryFocused

override fun setOnQueryTextFocusChangeListener(listener: OnFocusChangeListener?) {
    super.setOnQueryTextFocusChangeListener { v, hasFocus ->
        isQueryFocused = hasFocus
        listener?.onFocusChange(v, hasFocus)
    }
}

The other answers use recursive calls which is not a very efficient and private resource ID (the inner TextView id) which is a bad practice.