Android中的searchView不在操作栏中,如何移除焦点?

6

好的,这个问题已经被问过很多次了,我查看了很多相关主题但是我无法使它工作。

我的searchView不在操作栏上,因此我无法使用collapseActionView()

我尝试过

searchView.clearFocus();//this closes the keyboard but does not remove focus
getActivity().getCurrentFocus().clearFocus(); //basically the same as above
searchView.setQuery("", false); //this clears the query and drops the keyboard
searchView.setIconified(true);  // but the search retains focus and keyboard pops up again

无论我尝试什么,选中searchView后都无法取消焦点。

1
你尝试过将焦点设置到其他虚拟视图上吗?请查看以下链接: https://dev59.com/am025IYBdhLWcg3wQDlb - Ramesh
@Ramesh非常感谢,那个线程帮了我很大的忙。我先尝试了虚拟视图,但没有成功。可能是我做错了什么。我通过将我的布局设置为可聚焦并在进行搜索查询时设置它来使其工作。 - Gooner
希望能够将可行的解决方案发布为答案 :) - Ramesh
1个回答

10

在执行查询后,要清除我的searchView的焦点,我首先需要添加

    android:focusable="true"
    android:focusableInTouchMode="true"

添加到我的活动的父级布局中。然后在活动中

    LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.my_layout);
    myLayout.requestFocus();

这将使焦点离开搜索视图。我需要的所有信息都在这里如何在不设置另一个控件的焦点的情况下移除焦点?


11
android:focusable="false" 意为不可聚焦,android:focusableInTouchMode="true" 意为可在触摸模式下聚焦。 - Amith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接