2010-12-16 25 views
8

我試圖實現搜索對話框,我無法顯示來自活動的搜索。如何使用onSearchRequested()調用搜索對話框()

我有我的清單文件中定義的主要活動,此活動向用戶顯示他們必須從中選擇的選項列表。其中一個選項是搜索選項。

<activity 
     android:name=".MenuListActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

我的搜索活動在我的清單文件中定義如此。

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

現在我的問題是,當我從我的MenuListActivity調用onSearchRequested()沒有任何反應。

public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) { 
      String strText = items[position]; 

      if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_browse))) { 
       startActivity(new Intent(MenuListActivity.this, WSMobileActivity.class)); 
      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_search))) { 

      // If i call it like this it doesn't work, nothing happens 
       onSearchRequested(); 

      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_locator))) { 
       startActivity(new Intent(MenuListActivity.this, StoreLocatorActivity.class)); 
      } 
     } 

請幫忙,我該如何從我的MenuActivity調用搜索請求?

+0

你能接受一些答案作爲「答案」嗎? – motiver 2013-01-22 23:43:06

回答

3

除了在清單文件中聲明SearchActivity之外,還需要包含元數據信息。

如果你想調用整個應用程序的搜索對話框,然後包括

<meta-data android:name="android.app.default_searchable" 
       android:value=".SearchActivity" /> 

應用標籤內。

如果希望只在特定的活動來調用搜索對話框,然後包括

<meta-data android:name="android.app.default_searchable" 
       android:value=".SearchActivity" /> 

活動標籤內。

更多詳情請參考 http://developer.android.com/guide/topics/search/search-dialog.html

+0

我已經嘗試將其添加到活動和應用程序中。沒有任何作品適合我。 – 2011-08-16 15:21:38

23

你檢查了你的res/xml/searchable.xml嗎?

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

當您對提示和標籤進行硬編碼的字符串時,不會顯示搜索對話框。他們必須是@string資源。

另外,在調用活動中不需要調用或覆蓋onSearchRequested()方法,除非您想從您的UI小部件之一(如Button或ListItem)調用搜索。

+1

這讓我瘋狂,謝謝你的提示! – steemcb 2012-01-25 12:28:09

+0

謝謝_so_多!它是否記錄在某處?我陷入了這個問題很長一段時間... – ewino 2012-06-27 09:33:40

+0

很高興能夠提供幫助。我在我的應用程序中花了近一週的時間解決了這個問題我發現它很難。 – motiver 2012-07-09 16:10:28

0

您在清單中的活動標記中缺少標記。按照以下內容替換您的活動聲明

<activity 
     android:name=".MenuListActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

     <meta-data 
       android:name="android.app.default_searchable" 
       android:value=".SearchActivity" /> 

    </activity> 
0

我所做的一切,但仍然無法看到API 17.它的工作原理搜索對話框添加

後的Android然而:actionViewClass = 「android.widget.SearchView」

片段菜單

<item 
    android:id="@+id/menu_item_search" 
    android:actionViewClass="android.widget.SearchView" 
    android:icon="@android:drawable/ic_menu_search" 
    android:showAsAction="ifRoom" 
    android:title="@string/search"/> 
相關問題