如何取消 SearchView 的焦点?

44

我想在onResume()中从SearchView中移除焦点和文本。

我尝试使用searchView.clearFocus(),但它不起作用。

这是我的xml代码:

<SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/toolbar"
    android:layout_alignTop="@+id/toolbar"
    android:layout_centerVertical="true"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp"
    android:iconifiedByDefault="false"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" />

它为什么不工作?是引起了某种错误还是什么都没有发生?请更详细地描述一下... - Vucko
展示你的代码吧,伙计 :) - tinku
@Vucko 实际上它没有显示任何错误,光标也没有消失。 - Pradeep Deshmukh
11个回答

66

我希望在onResume()中从SearchView中移除焦点和文本。

为了实现这个目的,您可以使用android:focusableInTouchMode属性设置根布局可触摸模式下具有焦点,并在onResume()中请求该View的焦点。

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusableInTouchMode="true">

    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"/>

</LinearLayout>

然后在您的Activity中:

private View rootView;
private SearchView searchView;

// ...

@Override
protected void onCreate(Bundle savedInstanceState) {

    // ...

    rootView = findViewById(R.id.root_layout);
    searchView = (SearchView) findViewById(R.id.search_view);
}

@Override
protected void onResume() {
    super.onResume();

    searchView.setQuery("", false);
    rootView.requestFocus();
}

如果我们在 onResume 中移除 SearchView 的焦点(或转移焦点),在某些手机上会看到键盘闪烁一秒钟。在 onPause 中清除焦点使它看起来更流畅。 - Ananth
如果你有一个适配器,你需要仔细检查两次。否则,你的应用程序可能会崩溃。 - Arda Kazancı

22

在活动中返回到父布局,在fragment中也要在framelayout中执行相同的操作。只需添加此属性:

android:focusableInTouchMode="true"

13

在onResume方法中使用此行代码

if (searchView != null) {
    searchView.setQuery("", false);
    searchView.clearFocus();
    searchView.onActionViewCollapsed();
}

它正在禁用我的 focusListner,而我并不想要这样。我只希望每当点击时都应该回来。并且当片段重新加载时它应该被移除。 - Pradeep Deshmukh
你的问题不是你现在所解释的那样。(我想在onResume()中从SearchView中移除焦点和文本,我尝试了searchView.clearFocus();但它没有起作用。) - Masum

12
在您的SearchView小部件中,只需添加:
  android:focusable="false"

完整的小部件代码

    <android.support.v7.widget.SearchView
        android:id="@+id/your_search_view_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:iconifiedByDefault="false"
        app:queryHint="Search something..." 
        android:focusable="false" />

1
嗯,这似乎完全没问题。让我想知道为什么其他答案都更加复杂。 - lasec0203
对我来说,这应该是被接受的答案,针对焦点部分。 关于“可聚焦”的文档摘录: 控制视图是否可以获得焦点。默认情况下,这是“auto”,让框架确定用户是否可以将焦点移动到视图上。通过将此属性设置为true,允许视图获得焦点。将其设置为“false”则不会使视图获得焦点。这个值不影响直接调用{@link android.view.View#requestFocus}的行为,无论如何都会请求焦点。它只影响焦点导航尝试移动焦点的位置。 - C.Schone

3

onResume()中使用searchView.clearFocus();有助于解决我的问题。


3

将以下代码写入 onResume() 方法中。

  searchView.setText("");    
  this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);     

2

我刚刚请求将焦点转移到其他视图组,这使搜索视图失去了焦点。就像包含搜索视图的父视图组(例如LinearLayout、RelativeLayout)中添加:

focusable = false; focusableInTouchMode=true

到父级视图组。


0

最简单(也是我认为唯一100%有效的)解决方案是创建一个不可见的LinearLayout。这个LinearLayout将从EditText中获取焦点。


0

您可以在onResume()函数中手动隐藏键盘,如下所示:

InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
        View view = context.getCurrentFocus();

        if (view == null) {
            view = new View(context);
        }
        if (view != null) {

            IBinder iBinder = view.getWindowToken();

             imm.hideSoftInputFromWindow(iBinder, 0);
         }

0

对于那些想要在启动Activity时摆脱焦点,同时SearchViewMenu中的人,我做了这3件事,可能只需要做几件事就可以解决:

1.- 创建一个SearchView的父类。我们称之为RafaSearchView

class RafaSearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : SearchView(context, attrs, defStyleAttr) {

    init {
        setIconifiedByDefault(false)
    }
}

2. 获取包含Toolbar的XML,并将touchable="false"focusableInTouchMode="true"设置为:

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/searchToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        android:focusable="false"
        android:focusableInTouchMode="true"
        android:theme="@style/ToolBarStyle"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

3.- 当我在 onCreateOptionsMenu(Menu) 中声明我的 RafaSearchView 时,请调用:

    searchView?.isIconified = false
    searchView?.clearFocus()

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