onSearchRequested()未激活搜索对话框

时间:2015-06-02 15:57:27

标签: android search-dialog

我的主要活动需要调用搜索并显示搜索结果。调用onSearchRequested()时,不会出现搜索对话框。

我发现了类似的问题并且遵循了每一个细节(我认为),但显然我还有其他错误。以下是我的实施内容。

AndroidManifest.xml的部分

<application
    android:label="@string/AppName"     
    android:launchMode="singleTop"
    ... >

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

    <activity
      android:name=".main.MainActivity"
      android:launchMode="singleTop" 
      ... >

      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>

      <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
      </intent-filter>

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

    </activity>
    ...
</application>

deep_search.xml

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

MainActivity的部分内容:

public class MainActivity extends Activity implements ...
{
  @Override
  protected void onNewIntent (Intent intent)
  {
    setIntent(intent);
    if (Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
      String query = intent.getStringExtra (SearchManager.QUERY);
      // Do work using string
    }
  }

  @Override
  public boolean onOptionsItemSelected (MenuItem item) 
  { 
    int selectedId = item.getItemId();
    switch (selectedId)
    { 
      case ...:

      case R.id.menu_search_deep:
          boolean launched = onSearchRequested();
          logI ("home page deep searched launched: " + launched); // log shows we got here
        }
        return true; 
    }

  ...
}

0 个答案:

没有答案