机器人:返回的搜索查询到当前活动机器人

2023-09-05 05:55:52 作者:派小星

我也跟着上http://developer.android.com/guide/topics/search/search-dialog.html在我的记事本应用程序中实现搜索功能。

I followed the steps described on http://developer.android.com/guide/topics/search/search-dialog.html to implement a search feature in my notepad application.

我的问题是,当我完成了寻找新的活动打开捕获我的搜索查询。但我真正想要的,是查询返回启动一个新的,以目前的活动吧。

My problem is, that when I finish the search a new activity opens capturing my search query. But what I really want, is the query returned to the current activity instead of starting a new one.

这可能吗?

更新:

AndroidManifest.xml中

AndroidManifest.xml



<activity android:name="MyNotepad"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.SEARCH"></action>
            </intent-filter>
            <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>
        </activity><activity android:name="Preferences" android:label="Preferences" >
</activity>

searchable.xml

searchable.xml



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

Java的code

JAVA-code



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_pad);
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        Toast.makeText(getApplicationContext(), query, Toast.LENGTH_LONG).show();
    }
}



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

即使我用它不工作在手机上的搜索按钮。为此我认为这个问题是在AndroidManifest.xml

Even if I use the search-button on the phone it doesn't work. I therefor believe that the problem is in the AndroidManifest.xml

推荐答案

在你的应用程序清单,你需要定义当前活动的可搜索的活动。

In your Application Manifest you need to define the current activity as a searchable activity.

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

您可以使用下面的code,这是从http://developer.android.com/guide/topics/search/search-dialog.html#LifeCycle

You then use the following code, which is from http://developer.android.com/guide/topics/search/search-dialog.html#LifeCycle

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    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
    }
}

您可以再使用字符串来重新加载您的活动,如果一个列表的活动,你可以叫你用来加载数据,并使用字符串中的code。

You can then use the string to reload your activity, if its a list activity you can call your code that you use to load data and use the string in that.

 
精彩推荐
图片推荐