2011-09-18 82 views
0

我有一个菜单项,设置Android的搜索插件选项使用搜索小工具为actionViewClass,看起来像这样:通过检索XML配置

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

正如你可以看到我想要做的事就像上面的getSearchableInfo方法传入我的可搜索配置文件的ID。

任何人都可以帮我吗?

回答

4

您需要创建一个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类。欲了解更多详细看here

而且其中包含搜索查看活动,您需要设置搜索的信息为:

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

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

希望这可以帮助你,祝你好运!