我怎么打开搜索查看编程?我怎么

2023-09-12 01:42:52 作者:纣王虽暴,却爱妲己

有了这个小部件而被称为搜索查看的动作条。 当它不使用时,它看起来是这样的:

There is this widget for the ActionBar which called '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").

我试了几个功能,如:

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

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

但没有这些工作...的

But none of those worked...

在XML中的搜索查看:

The SearchView in the XML:

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

感谢名单的前期。

Thanx upfront.

推荐答案

展开搜索查看

searchView.setIconified(false);

和折叠它

searchView.setIconified(true);

您需要改变的值安卓showAsAction ifRoom | collapseActionView 总是。该搜索查看的属性安卓iconifiedByDefault ,这是默认值,否则用户无法折叠搜索查看后,这是编程扩展。

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.