gpt4 book ai didi

android - 当前 Activity 未收到搜索 Intent

转载 作者:太空狗 更新时间:2023-10-29 16:16:18 24 4
gpt4 key购买 nike

我正在实现一个 Activity ,我想在其中添加一个搜索栏。我希望当前 Activity 能够接收回 Intent 。我遵循了几个可用的示例,并在我的应用程序中添加了以下代码:

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 Activity 中:

@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 Activity 中的 onNewIntent 没有收到按软键盘中的输入或搜索图标的调用。有人可以帮我解决这个问题吗?我在这里错过了什么?多谢!

最佳答案

经过更多搜索,我找到了解决问题的办法。

保持其他一切与我在上面发布的相同,我刚刚使用下面提供的代码更新了 Searchable Activity (在我的例子中是 BrowseCategory)中的 onCreateOptionsMenu(Menu menu) 方法:

@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 设置为 searchview 很重要。设置它现在触发 onNewIntent。如果有人对此有更多了解,请提供解释。如果我也找到更多相关信息,我会更新。

关于android - 当前 Activity 未收到搜索 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com