Android:将搜索查询返回到当前活动 [英] Android: Return search query to current activity
问题描述
我遵循了 http://developer.android 上描述的步骤.com/guide/topics/search/search-dialog.html 在我的记事本应用程序中实现搜索功能.
我的问题是,当我完成搜索时,会打开一个新活动来捕获我的搜索查询.但我真正想要的是,查询返回到当前活动而不是开始一个新活动.
这可能吗?
更新:
AndroidManifest.xml
<前><代码><activity android:name="MyNotepad";android:label=@string/app_name"><意图过滤器><action android:name="android.intent.action.MAIN";/><category android:name="android.intent.category.LAUNCHER";/><action android:name="android.intent.action.SEARCH></action></意图过滤器><meta-data android:resource="@xml/searchable";android:name="android.app.searchable"></meta-data></activity><activity android:name="Preferences";机器人:标签=首选项";></活动>searchable.xml
<前><代码><?xml version="1.0";编码=utf-8"?><可搜索的xmlns:android=http://schemas.android.com/apk/res/android"android:label="@string/app_name";android:hint="@string/search_hint"></可搜索的>JAVA 代码
<前><代码>@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_pad);意图意图 = getIntent();如果 (Intent.ACTION_SEARCH.equals(intent.getAction())) {字符串查询 = intent.getStringExtra(SearchManager.QUERY);Toast.makeText(getApplicationContext(), query, Toast.LENGTH_LONG).show();}}<前><代码>@覆盖public boolean onOptionsItemSelected(MenuItem item){开关(item.getItemId()){案例 R.id.menuItemSearch:startSearch("", false, null, false);休息;}返回真;}
即使我使用手机上的搜索按钮也不起作用.因此我认为问题出在 AndroidManifest.xml
在您的应用程序清单中,您需要将当前活动定义为可搜索活动.
<activity android:name="BrowseItems" android:label="@string/browseitems"android:launchMode="singleTop"><意图过滤器><action android:name="android.intent.action.SEARCH"/></意图过滤器><meta-data android:name="android.app.searchable"android:resource="@xml/itemsearchable"/></活动>
然后您使用以下代码,该代码来自 http://developer.android.com/guide/topics/search/search-dialog.html#LifeCycle
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.search);handleIntent(getIntent());}@覆盖受保护的无效 onNewIntent(意图意图){设置意图(意图);处理意图(意图);}私人无效handleIntent(意图意图){如果 (Intent.ACTION_SEARCH.equals(intent.getAction())) {字符串查询 = intent.getStringExtra(SearchManager.QUERY);//使用字符串做工作}}
然后您可以使用该字符串重新加载您的活动,如果它是一个列表活动,您可以调用您用来加载数据的代码并在其中使用该字符串.
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.
Is this possible?
UPDATE:
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
<?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
@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;
}
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>
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
}
}
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.
这篇关于Android:将搜索查询返回到当前活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!