【问题标题】:Search Interface opens a new Activity when search搜索界面在搜索时打开一个新的Activity
【发布时间】:2012-05-29 09:49:00
【问题描述】:

我正在尝试使用 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);
        }

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

谢谢。

【问题讨论】:

    标签: android android-searchmanager


    【解决方案1】:

    解决方案包括重用开发者示例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>
    ...
    

    【讨论】:

      【解决方案2】:

      在清单中使用

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-14
        相关资源
        最近更新 更多