2012-05-29 44 views
4

我正在尝试使用Android的搜索管理器。搜索界面在搜索时打开新的活动

其实,我在那里,我呼吁

onSearchRequested() 

接着一个活动,我正在使用相同的活动这个函数来获取搜索字符串:

// Get the intent, verify the action and get the query 
     Intent intent = getIntent(); 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      String query = intent.getStringExtra(SearchManager.QUERY); 
     } 

问题是接下来:当我点击搜索按钮时,我打开一个新的活动,我想保持不变,并进行一些搜索。因此,我的目标是在点击搜索按钮时避免新的活动开放。

谢谢。

回答

4

该解决方案由重用developpers示例http://developer.android.com/guide/topics/search/search-dialog.html

重要的是创造2个活动,一个用于搜索和一个其它用于显示结果。

在清单中,使用以下代码:

<application ... > 
<!-- this is the searchable activity; it performs searches --> 
<activity android:name=".SearchableActivity" > 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH" /> 
    </intent-filter> 
    <meta-data android:name="android.app.searchable" 
       android:resource="@xml/searchable"/> 
</activity> 

<!-- this activity enables the search dialog to initiate searches 
    in the SearchableActivity --> 
<activity android:name=".OtherActivity" ... > 
    <!-- enable the search dialog to send searches to SearchableActivity --> 
    <meta-data android:name="android.app.default_searchable" 
       android:value=".SearchableActivity" /> 
</activity> 
... 

0

使用在清单

<activity android:name="BrowseItems" android:label="@string/browseitems" 
      android:launchMode="singleTop"> 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
      </intent-filter> 
      <meta-data android:name="android.app.searchable" 
       android:resource="@xml/itemsearchable" /> 
</activity>