Android SearchView OnFocusChangeListener:根本不调用onFocusChange [英] Android SearchView OnFocusChangeListener: onFocusChange is not called at all

查看:397
本文介绍了Android SearchView OnFocusChangeListener:根本不调用onFocusChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动中有一个SearchView;当用户执行搜索时,包含搜索结果的Fragment将添加到同一Activity中.

I have a SearchView in an Activity; when the user executes search, a Fragment containing the search results is adding to the same Activity.

现在我想要的是,当单击SearchView(以便它获得焦点并在其中键入用户)时,已经显示的Fragment(包含搜索结果)应该变暗/模糊

Now what I want is that when the SearchView is clicked (so it gets the focus, and the user types there), the Fragment already displayed (containig the search results) should become dim/blurred.

因此,我尝试在FragmentonActivityCreated生命周期回调方法中为SearchView设置OnFocusChangedListener,但似乎根本没有调用它.

So I tried to set an OnFocusChangedListener for the SearchView in the onActivityCreated life cycle callback method of the Fragment, but it does not seem to be called at all.

final Activity myActivity = getActivity();
        Log.i(TAG, "myActivity>"+myActivity.toString());//check
        SearchView mySearchView = (SearchView) myActivity.findViewById(R.id.searchActivity_searchView);
        Log.i(TAG, "mySearchView>"+mySearchView.toString());//check
        mySearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
            @SuppressLint("NewApi")
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i(TAG, "onFocusChange OF OnFocusChangeListener IN SearchResultsFragment CALLED.");
                if (hasFocus) {
                    myActivity.getWindow().setDimAmount(1.0f);
                }
            }
        });

那么我想念什么吗? 当用户单击并激活SearchView以便在其中键入其搜索查询时,该如何调用onFocusChanged?

So am I missing something? What should I do to get the onFocusChanged called when the user clicks and thus activites the SearchView to type their Search query in it?

注意:如果需要完整的SSCCE代码,请在评论中提出,我将其发布.

Note: If an entire SSCCE's code is required, please ask in comments, I'll post it.

推荐答案

很抱歉无法提供最新答案,但是我遇到了同样的问题,并且在搜索过程中发现了这个问题,因此它可能会对其他人有所帮助.

Sorry for late answer, but I had the same problem and found this question during search, so it might perhaps help other people.

您尝试使用mySearchView.setOnQueryTextFocusChangeListener()代替mySearchView.setOnFocusChangeListener()吗?

为我工作.

这篇关于Android SearchView OnFocusChangeListener:根本不调用onFocusChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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