通过 Searchable xml 配置设置 Android 搜索小部件选项

发布于 2024-12-05 08:00:37 字数 1041 浏览 0 评论 0原文

我有一个菜单项使用搜索小部件作为其 actionViewClass ,如下所示:

     <item android:id="@+id/menu_search"
        android:title="Search"
        android:icon="@drawable/ic_menu_search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView" />      

它工作正常,但是我似乎在 android 网站文档中提到了可搜索的 xml 配置文件,如下所示:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="string resource"
    android:hint="string resource" />

我怎样才能获取菜单项搜索小部件以使用上述配置?我认为这与 SearchManager.setSearchableInfo 方法有关,我在代码中获取了搜索小部件的句柄,如下所示:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search).getActionView();       

searchItem.setSearchableInfo(searchManager.getSearchableInfo(R.layout.searchable));

如您所见,我想使用 < code>getSearchableInfo 方法并传入我的可搜索配置文件的 id。

有人可以帮我吗?

I have a menu item uses the search widget as its actionViewClass that looks like so:

     <item android:id="@+id/menu_search"
        android:title="Search"
        android:icon="@drawable/ic_menu_search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView" />      

It works fine, however I have seem mention on the android site docs of a searchable xml config file like this:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="string resource"
    android:hint="string resource" />

How can I get the menu item search widget to use the above configuration?? I assume it is something to do with the SearchManager.setSearchableInfo method, I am getting a handle to the search widget in my code like this:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search).getActionView();       

searchItem.setSearchableInfo(searchManager.getSearchableInfo(R.layout.searchable));

As you can see I want to do something like above with the getSearchableInfo method and pass in the id of my searchable config file.

Can anyone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您需要创建一个SearchActivity,它处理来自搜索小部件的搜索查询并显示搜索结果,并且您还需要在android清单文件中声明它。

搜索活动的示例:

public class SearchEngine extends Activity
{

TextView result;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.search_view);
    result=(TextView)findViewById(R.id.textView1);
    this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);

    final Intent queryIntent = getIntent();
    final String queryAction = queryIntent.getAction();

    if(Intent.ACTION_SEARCH.equals(queryAction)) 
    {
        this.doSearchQuery(queryIntent);
    } 
    else if (Intent.ACTION_VIEW.equals(queryAction)) 
    {
        this.doView(queryIntent);
    } 
    else
    {
        Log.d("Intent", "Create intent NOT from search");
    }


}

@Override
public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    final String queryAction = intent.getAction();
    if (Intent.ACTION_SEARCH.equals(queryAction))
    {
        this.doSearchQuery(intent);
    }
    else if (Intent.ACTION_VIEW.equals(queryAction)) 
    {
        this.doView(intent);
    }
}
public void doSearchQuery(Intent intent)
{
     String queryString = intent.getDataString(); // from suggestions
        if (queryString == null) 
        {
            queryString = intent.getStringExtra(SearchManager.QUERY);
               // from search-bar
        }

        finish();
}
public void doView(Intent intent)
{
      Log.e("action view"," suggestion ");

}


}

在清单中:

  <activity android:name=".SearchActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
         <meta-data android:name="android.app.searchable"
                 android:resource="@xml/searchable" />
    </activity>

您需要将搜索配置文件“searchable.xml”放入文件夹 res/xml 中。
searchable.xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>

   <searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search"
    android:searchMode="showSearchLabelAsBadge"
    android:searchSettingsDescription="Custom Suggestions"
    android:queryAfterZeroResults="false"
    android:searchSuggestAuthority="com.example.customsearch.CustomSearchSuggestion"
    android:searchSuggestSelection=" ? "
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:voiceSearchMode="showVoiceSearchButton"

    >
  </searchable>

为了提供自定义搜索建议,您需要创建一个扩展 ContentProvider 或 SearchRecentSuggestionsProvider 的类。 ,请参阅此处

有关更多详细信息 在搜索视图中,您需要将可搜索信息设置为:

 SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);

 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

希望这对您有帮助,祝您好运!

You need to create a SearchActivity, which process the search queries from the search widget and show the search results and also you need to declare it in the android manifest file.

An example of search activity:

public class SearchEngine extends Activity
{

TextView result;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.search_view);
    result=(TextView)findViewById(R.id.textView1);
    this.setDefaultKeyMode(Activity.DEFAULT_KEYS_SEARCH_LOCAL);

    final Intent queryIntent = getIntent();
    final String queryAction = queryIntent.getAction();

    if(Intent.ACTION_SEARCH.equals(queryAction)) 
    {
        this.doSearchQuery(queryIntent);
    } 
    else if (Intent.ACTION_VIEW.equals(queryAction)) 
    {
        this.doView(queryIntent);
    } 
    else
    {
        Log.d("Intent", "Create intent NOT from search");
    }


}

@Override
public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    final String queryAction = intent.getAction();
    if (Intent.ACTION_SEARCH.equals(queryAction))
    {
        this.doSearchQuery(intent);
    }
    else if (Intent.ACTION_VIEW.equals(queryAction)) 
    {
        this.doView(intent);
    }
}
public void doSearchQuery(Intent intent)
{
     String queryString = intent.getDataString(); // from suggestions
        if (queryString == null) 
        {
            queryString = intent.getStringExtra(SearchManager.QUERY);
               // from search-bar
        }

        finish();
}
public void doView(Intent intent)
{
      Log.e("action view"," suggestion ");

}


}

and in the manifest:

  <activity android:name=".SearchActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
         <meta-data android:name="android.app.searchable"
                 android:resource="@xml/searchable" />
    </activity>

You need to put the search configuration file 'searchable.xml' in the folder res/xml .
The searchable.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>

   <searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search"
    android:searchMode="showSearchLabelAsBadge"
    android:searchSettingsDescription="Custom Suggestions"
    android:queryAfterZeroResults="false"
    android:searchSuggestAuthority="com.example.customsearch.CustomSearchSuggestion"
    android:searchSuggestSelection=" ? "
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:voiceSearchMode="showVoiceSearchButton"

    >
  </searchable>

And to provide custom search suggestions, you need to create a class which extends ContentProvider or SearchRecentSuggestionsProvider. For more details look here

Also in the activity which contains the search view, you need to set the searchable info as:

 SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);

 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

Hope this helps you, Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文