2

收到我實現一個活動,我想添加一個搜索欄。我希望當前的活動能夠收回意圖。我跟着可用的幾個例子,在我的應用程序添加下面的代碼:搜索意圖不是當前活動

AndroidManifest.xml中

<activity 
     android:name=".search.BrowseCategory" 
     android:launchMode="singleTop" 
     android:windowSoftInputMode="stateHidden"> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 

我加在AndroidManifest.xml以下下的應用程序標籤:

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

現在我的BrowseCategory活動:

@Override 
protected void onNewIntent(Intent intent) { 
    Log.d(getPackageName(), "Search intent received!"); 
    setIntent(intent); 
    handleIntent(intent); 
} 

private void handleIntent(Intent intent) { 
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     // Do work using string 
     doSearch(query); 
    } 
} 

private void doSearch(String query) {...} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.search_filter, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    switch(item.getItemId()) 
    { 
     case R.id.search: 
      startSearch("", false, null, false); 
    break; 
    } 
    return true; 
} 

搜索菜單

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
<item 
    android:id="@+id/search" 
    android:actionViewClass="android.widget.SearchView" 
    android:showAsAction="ifRoom" 
    android:title="@string/search"/> 
</menu> 

檢索XML

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

有了這個,我得到我的動作條搜索字段。但是,我在BrowseCategory活動中的onNewIntent沒有通過按軟鍵盤上的輸入或搜索圖標獲得呼叫。有人可以幫助我解決這個問題嗎?我在這裏錯過了什麼?非常感謝!

+0

是否使用'SearchView'? – Gattsu 2014-09-30 06:51:38

+0

是的,在動作條 Android上搜索查看窗口小部件:actionViewClass =「android.widget.SearchView」 – Keya 2014-09-30 06:56:31

+0

你基本上要接收的意圖在同一個活動? – 2014-09-30 07:00:31

回答

3

搜索更多後,我發現了一個解決我的問題。

保持一切同我在上面貼出來,我剛剛更新了我的onCreateOptionsMenu(菜單菜單)方法中檢索的活動(BrowseCategory在我的情況)的代碼提供如下:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.search_filter, menu); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView search = (SearchView) menu.findItem(R.id.search).getActionView(); 
     search.setIconifiedByDefault(false); 
     search.setSearchableInfo(manager.getSearchableInfo(getComponentName())); 
     search.setOnQueryTextListener(new OnQueryTextListener() { 
      @Override 
      public boolean onQueryTextChange(String query) { 
       doSearch(query); 
       return true; 
      } 

      @Override 
      public boolean onQueryTextSubmit(String query) { 
       return false; 
      } 
     }); 
    } 
    return true; 
} 

OnQueryTextListener是關鍵在這裏!添加此監聽器會觸發我的搜索,現在甚至可以在軟鍵盤上調用onNewIntent時按「搜索」或「開始」。關於以後的問題,從回答的一個閱讀Cannot get searchview in actionbar to work我相信設置權SearchableInfo到搜索查看是很重要的。現在觸發onNewIntent。如果有人知道更多,請提供解釋。我會更新,如果我也發現更多。

3

在BrowseCategory活動:

將此塊添加

@Override 
protected void onNewIntent(Intent intent) { 
    if (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) { 
     //handles suggestion clicked query 
     String displayName = getDisplayNameForContact(intent); 
     resultText.setText(displayName); 
    } 
    //this else if is called when you will press Search Icon or Go (on soft keyboard) 
    else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     // handles a search query 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     resultText.setText("should search for query: '" + query + "'..."); 
    } 
} 

之前點擊

enter image description here

點擊後

enter image description here

+0

感謝您的回覆!我的Activity中覆蓋了一個onNewIntent()方法。然而,意圖本身沒有得到收到。這是主要問題。我還應該做些什麼才能讓意圖解僱? – Keya 2014-09-30 07:15:08

+0

嘗試我建議的方式:),並顯示你的'搜索。xml' @Keya – Gattsu 2014-09-30 07:17:29

+0

用searchable.xml內容更新了問題。 onNewIntent根本沒有被調用。 – Keya 2014-09-30 07:25:25