如何以编程方式打开 SearchView?

有一个叫做“ SearchView”的操作栏小部件。 不使用的时候,它看起来像这样:

enter image description here

当它被使用的时候,它看起来像这样:

enter image description here

我想(当然是以编程的方式)打开 searchview (使其“正在使用”)。

我尝试了以下几种功能:

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setOnQueryTextListener(this);


searchView.performClick();
searchView.requestFocus();

但这些都不管用。

XML 中的 SearchView:

<item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
59724 次浏览

Expand the SearchView with

searchView.setIconified(false);

and collapse it with

searchView.setIconified(true);

You need to change the value of android:showAsAction from ifRoom|collapseActionView to always. The SearchView's attribute android:iconifiedByDefault should be true, which is the default value, otherwise the user can not collapse the SearchView after it was expanded programmatically.

I know this is late but

Try calling expandActionView() to open it and collapseActionView() to close it. You can call requestFocus() on the actual Action View via getActionView() to give the search view focus :)

Try to call expandActionView() on MenuItem, not onActionViewExpanded() on ActionView.

It works for me.

MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchMenuItem.expandActionView();

If you want to use support library only when necessary, do this

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
if (Utils.hasIceCreamSandwich())
searchMenuItem.expandActionView();
else MenuItemCompat.expandActionView(searchMenuItem);

else simply do this

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
MenuItemCompat.expandActionView(searchMenuItem);

To open up a searchView keeping the close_button in the end use this in onCreate:-

searchView.findViewById(R.id.search_button).performClick();

Expanding the answer of Matthias Robberts:

I wanted a list fragment to keep it's search value and set it after user returns from other fragment.

if (myViewModel.filterTextSaved.isNotEmpty()) { // Kotlin, storing state in ViewModel
searchItem.expandActionView() // needs to come first, otherwise empty text
theTextArea.setText(courseViewModel.filterTextOnClick)
}

and for the menu I keep always|collapseActionView, otherwise it stays open when user deletes the text.

I was searching for a solution to expand SearchView programmatically because I need to restore expand state. No one solution was worked for me. Except using Handler().post(). I know it's not a good practice to use post(). But I'm working on legacy project and this workaround is acceptable for me.

Initialize variables block

val searchMenuItem = menu.findItem(R.id.menu_search_item)
val searchView = searchMenuItem.actionView as SearchView

How to expand SearchView:

Handler().post {
searchMenuItem.expandActionView()
}

Reason why post() helps: if your app is not well written it may doing too much work on UI thread. post() ensures that your block of code will be executed when UI thread will be available for execution. Which means if you are using this workaround you can notice small delay between SearchView expanding

For androidx.appcompat.widget.SearchView,

searchView.setIconifiedByDefault(true)   // didn't work


searchMenuItem.expandActionView()  // didn't work


MenuItemCompat.expandActionView(searchMenuItem) // didn't work


searchView.onActionViewExpanded()  // didn't work

The following worked for me,

searchView.findViewById<View>(R.id.search_button).performClick()