显示SearchWidget在动作条,如果用户presses设备的搜索按钮按钮、动作、用户、设备

由网友(你不是我,自然不会懂我)分享简介:我成立了一个SearchWidget如 Android的API指南描述。它在操作栏可以正确地显示一个放大镜图标,如果我点击它,它会启动操作栏(与深色背景的输入字段)。I set up a SearchWidget as described in the Android API Guide. It properly d...

我成立了一个SearchWidget如 Android的API指南描述。它在操作栏可以正确地显示一个放大镜图标,如果我点击它,它会启动操作栏(与深色背景的输入字段)。

I set up a SearchWidget as described in the Android API Guide. It properly displays a magnifying glass icon in the Action Bar and if I click on it, it launches the search widget within the Action Bar (an input field with dark background).

不过,如果我preSS虚拟设备的搜索按钮,然后不同的搜索领域推出:它有一个白色背景,并不会调用我指定的回调方法

However, if I press the virtual device's search button, then a different search field is launched: It has a white background and does not invoke the callback methods I specified.

所以我说这我MainActivity类:

So I added this to my MainActivity class:

public boolean onSearchRequested() {
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setIconified(!searchView.isIconified());
    return false;
}

搜索小工具的设立是完全一样的,因为它在文档中描述:

The set up of the search widget is exactly the same as it is described in the docs:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new CustomSearchQueryTextListener());

我的searchable.xml:

My searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name">
</searchable>

和AndroidManifest.xml中:

And AndroidManifest.xml:

    <activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:uiOptions="splitActionBarWhenNarrow" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
    </activity>

为什么我试过setIconfied()方法的原因是,Android的API中引导它的切换搜索框的可见性说:

The reason why I tried the setIconfied() method is that in the Android API Guide it says under "The ability to toggle the search box visibility":

默认情况下,搜索窗口小部件图标化,这意味着它是   重新presented仅由搜索图标(放大镜),并扩展到   显示搜索框当用户触摸它。如上图所示,你可以   默认显示的搜索框,通过调用   setIconifiedByDefault(假)。你也可以切换搜索小工具   外观致电setIconified()。

"By default, the search widget is "iconified," meaning that it is represented only by a search icon (a magnifying glass), and expands to show the search box when the user touches it. As shown above, you can show the search box by default, by calling setIconifiedByDefault(false). You can also toggle the search widget appearance by calling setIconified()."

我也试过打电话开始搜索(,假,空,假); 但是这带来了白色背景相同的搜索字段

I also tried to call startSearch("", false, null, false); but this brings up the same search field with the white background.

所以我的问题是:是否有可能启动搜索小工具,如果用户presses设备的搜索按钮的动作条中,即peform完全相同的动作,仿佛在搜索菜单项被点击?

So my question is: Is it possible to launch the search widget within the action bar if the user presses the device's search button, i.e. peform exactly the same action as if the search menu item is clicked?

推荐答案

在写这个问题,我想出了一个解决方案,由我自己。我还是决定将它张贴连同答案,因为它似乎并不像这个问题已经被问在此之前,也许有人遇到同样的问题,和/或来了一个更好的解决方案。

While writing this question I came up with the solution by myself. I still decided to post it along with the answer, because it does not seem like this question has been asked on SO before and maybe someone encounters the same problem and/or comes up with a better solution.

所有你需要做的就是调用 expandActionView()在搜索菜单项,然后一切工作像预期的:

All you need to do is call expandActionView() on the search menu item, and everything works like expected:

public boolean onSearchRequested() {
    menu.findItem(R.id.menu_search).expandActionView();
    return true;
} 
阅读全文

相关推荐

最新文章