Android searchView 不在 actionBar 中移除焦点

标签 android searchview

好吧,这个问题已经被问过很多次了,我已经浏览了很多关于它的主题,但我无法让它工作。

我的 searchView 不在 actionBar 上,所以我不能使用 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 后将其移走

最佳答案

为了在执行查询后从我的 searchView 中清除焦点,我首先必须添加

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

到 Activity 的父级布局。然后在 Activity 中

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

这会将焦点从 searchView 上移开。我需要的所有信息都在这里 How to remove focus without setting focus to another control?

关于Android searchView 不在 actionBar 中移除焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32563377/

相关文章:

android - 对应新GCM中的GCMRegistrar.isRegistered()

android - 默认 Activity

android - PreferenceScreen - <intent .../> - 异常 - FLAG_ACTIVITY_NEW_TASK

android - KotlinNullPointerException : inside onCreateOptionsMenu can't find searchView; android

android - SearchView Edittext 不显示光标

android - 在 JNI 中使用错误代码将 jbytearray 转换为 char*

android - Google Cloud API 在 APK com/google/cloud/project.properties 中复制的重复文件

java - Android 自定义 ListView 与图片和子项目搜索 View 不起作用

android - SearchView 建议 - 布局宽度 : match_parent

android - 在android搜索 View 中隐藏关闭按钮