无法让actionbar中的searchview工作
-
13-12-2019 - |
题
我对java和android开发很陌生;我一直在开发一个应用程序,在这个应用程序中,我想要一个带有 SearchView
.我已经看过谷歌的例子,但我不能得到它的工作。我一定是做错了什么,我即将放弃应用程序开发:D我已经搜索了,但我没有发现任何有用的东西。也许你们可以帮助我:)
问题出在哪里:我得到搜索视图打开,因为它应该,但在那之后什么都没有发生,我注意到我的 搜索管理员。getSearchableInfo(getComponentName()) 返回null。我给出的提示也没有显示在我的搜索框中,这使我相信也许应用程序找不到我的搜索。xml?足够的谈话:)
主要活动。java语言
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
可搜索。xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_label">
</searchable>
Androidmanifest,Androidmanifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</application>
</manifest>
SearchableActivity。java语言
package com.example.searchapp;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class SearchableActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// 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);
doMySearch(query);
}
}
private void doMySearch(String query) {
Log.d("Event",query);
}
}
这里是一个链接到提到的项目,如果有人帮助我,我将永远感激!
解决方案
您的问题位于您的AndroidManifest中。我几乎与您完全一样,因为这就是我在文档中得到的。但它尚不清楚或错误。
在API Demos源上观看我发现“android.app.searchable”元数据必须进入您的“结果”活动,以及在主要活动中(或您放置搜索视图的活动)您指向另一个一个与“android.app.default_searchable”。
您可以在以下文件中看到它,这是来自我的测试项目的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity
android:name=".SearchActivity"
android:label="@string/app_name" >
<!-- This intent-filter identifies this activity as "searchable" -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<!-- This metadata entry provides further configuration details for searches -->
<!-- that are handled by this activity. -->
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
.
也很重要的是,搜索条目中的提示和标签是引用,而不是硬件字符串。
我希望它解决你的问题。我花了我一整天来搞清楚:(
其他提示
Android指南明确说:
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
.
在您的情况下不是真的。因为你不想搜索主动,但在搜索中。这意味着您应该使用此ComponentName而不是GetComponentName()方法:
ComponentName cn = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
. 似乎也是android:提示和android:label属性必须是trings.xml中字符串的引用。懒惰和硬编码字符串值似乎根本不起作用:)
i.e。不要做:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="Search for something..."
android:label="My App">
</searchable>
.
但绝对是(正如正确的方式):
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_label">
</searchable>
. 如果您使用的是Android Studio,
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
.
部分可能无法工作。
写入完整的包名称,如
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.test.SearchActivity" />
. 小心"xml/可搜索。xml"。如果你用硬编码 android:hint
和 android:label
。它不会工作。哈哈
应该是 :
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/app_name"
android:label="@string/search_lb"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
实际上没有必要使用上面答案中描述的"缺失部分"。此方法(使用 SearchView
)完全按照官方文档所说的工作。 使用搜索小部件.这里需要稍作修改,如下所示:
A中的属性 SearchableInfo
用于显示标签、提示、建议、创建启动搜索结果屏幕的意图和控制其他功能,如语音按钮(文件).
因此, SearchableInfo
对象我们传递给 SearchView.setSearchableInfo(SearchableInfo s)
必须包含对目标活动的适当引用,这些引用应该显示以显示搜索结果。这是通过手动传递 ComponentName
我们的目标活动。创建一个构造函数 ComponentName
是
public ComponentName (String pkg, String cls)
pkg
和 cls
是要启动的目标活动的包名称和类名称,用于显示搜索结果。请注意:
pkg
必须指向项目的根包名称和,
cls
必须是类的完全限定名称(即整件事)
例如:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
// Get the SearchView and set the searchable configuration
String pkg = "com.example.musicapp"
String cls = "com.example.musicapp.search.SearchActivity"
ComponentName mycomponent = new ComponentName(pkg,cls);
SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(mycomponent));
searchView.setIconifiedByDefault(false); //if using on actionbar
return true;
}
根据您的要求,看看其他 ComponentName
构造函数。
确保你的 searchables.xml
文件在正确的位置(即,在您的 res/xml/
文件夹)并且同一文件不包含任何错误-否则您会发现 (SearchManager)getSystemService(Context.SEARCH_SERVICE).getSearchableInfo(componentName)
将返回null,破坏您的搜索功能。
(另外,确保您设置 componentName
以适当的方式,因为Android指南中显示的示例仅适用于您进行搜索并在同一活动中显示搜索时。)
我面临着同样的问题,将这样的搜索意图过滤器添加到可搜索的活动:
<activity
android:launchMode="singleTop"
android:name=".ui.activities.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<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/searchable" />
</activity>
. 仔细检查相应的条目(IE Search_hint和app_label)是否存在于值/ strings.xml
中,例如
<string name="app_label">FunApp</string>
<string name="search_hint">Caps Off</string>
.