Android SearchView OnFocusChangeListener: onFocusChange is not called at all

丶灬走出姿态 提交于 2019-12-06 18:21:48

问题


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

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.

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

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?

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


回答1:


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

Have you tried using mySearchView.setOnQueryTextFocusChangeListener() instead of mySearchView.setOnFocusChangeListener()?

Worked for me.



来源:https://stackoverflow.com/questions/27944238/android-searchview-onfocuschangelistener-onfocuschange-is-not-called-at-all

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