我怎么打开搜索查看编程? [英] How do I open the SearchView programmatically?

查看:152
本文介绍了我怎么打开搜索查看编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于我怎么打开搜索查看编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆