SearchView.isFocused始终返回false [英] SearchView.isFocused always returns false

查看:144
本文介绍了SearchView.isFocused始终返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定ActionBar中的SearchView是否聚焦.但是,当我呼叫SearchView.isFocused()时,即使视图真正被聚焦(里面有一个光标,并且显示了软键盘),我也总是得到false的结果.

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).

如何检查SearchView是否聚焦?

推荐答案

经过研究,我了解到SearchView.isFocused()总是返回false,因为这是SearchView的某个孩子真正具有焦点,而不是SearchView本身.因此,我使用以下代码检查SearchView的焦点:

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;
}

所以我打电话给checkFocusRec(searchView)来检查焦点.我不确定这是否是最佳解决方案,但它对我有用.

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

这篇关于SearchView.isFocused始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆