简体   繁体   English

如何以编程方式打开 SearchView?

[英]How do I open the SearchView programmatically?

There is this widget for the ActionBar which called 'SearchView'. ActionBar 有一个小部件,称为“SearchView”。 When it's not in use, it looks like this:当它不使用时,它看起来像这样:

在此处输入图片说明

And when it's in use, it looks like this:当它在使用时,它看起来像这样:

在此处输入图片说明

I want (programmatically of course) to open the searchview (make it "in use").我想(当然以编程方式)打开搜索视图(使其“使用中”)。

I tried several functions such as:我尝试了几个功能,例如:

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

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

But none of those worked...但这些都没有用……

The SearchView in the XML: 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" />

Expand the SearchView with展开SearchView

searchView.setIconified(false);

and collapse it with并将其折叠

searchView.setIconified(true);

You need to change the value of android:showAsAction from ifRoom|collapseActionView to always .您需要将android:showAsAction的值从ifRoom|collapseActionView更改为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. SearchView的属性android:iconifiedByDefault应该是true ,这是默认值,否则用户无法通过编程方式展开SearchView后折叠。

Try to call expandActionView() on MenuItem, not onActionViewExpanded() on ActionView.尝试在 MenuItem 上调用expandActionView() ,而不是在 ActionView 上调用 onActionViewExpanded()。

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

I know this is late but我知道这已经晚了但是

Try calling expandActionView() to open it and collapseActionView() to close it.尝试调用 expandActionView() 来打开它并调用 collapseActionView() 来关闭它。 You can call requestFocus() on the actual Action View via getActionView() to give the search view focus :)您可以通过 getActionView() 在实际操作视图上调用 requestFocus() 以提供搜索视图焦点:)

For androidx.appcompat.widget.SearchView ,对于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()

Expanding the answer of Matthias Robberts:扩展 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.对于菜单,我always|collapseActionView保持always|collapseActionView ,否则当用户删除文本时它会保持打开状态。

要打开一个searchView保持close_button最后在onCreate使用它:-

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

I was searching for a solution to expand SearchView programmatically because I need to restore expand state.我正在寻找一种以编程方式扩展 SearchView 的解决方案,因为我需要恢复扩展状态。 No one solution was worked for me.没有一种解决方案对我有用。 Except using Handler().post().除了使用 Handler().post()。 I know it's not a good practice to use post().我知道使用 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:如何扩展 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() 有帮助的原因:如果你的应用程序写得不好,它可能会在 UI 线程上做太多工作。 post() ensures that your block of code will be executed when UI thread will be available for execution. post() 确保您的代码块将在 UI 线程可用于执行时执行。 Which means if you are using this workaround you can notice small delay between SearchView expanding这意味着如果您使用此解决方法,您会注意到 SearchView 展开之间的小延迟

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM