【发布时间】:2012-04-13 01:29:11
【问题描述】:
我正在使用可搜索的操作栏。而且我遇到了一个问题,即操作栏不会对可搜索的活动做出反应。也就是说,我在操作栏的可搜索文本编辑中输入了一些内容,然后单击了提交,但没有任何反应。调试跟踪显示我的 searchresultactivity 中的代码从未执行过。所以我想知道可搜索配置是否有问题。嗯,这一次我想我已经把问题解释清楚了,我不希望任何没有任何言论的反对票甚至吐!
我按照manifest.xml开头的开发者文档的说明,在searchresult活动中添加元数据:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".GermanDictionaryActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
然后我用我的菜单初始化了搜索视图:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.srhbar, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSrhView = (SearchView) menu.findItem(R.id.search).getActionView();
mSrhView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSrhView.setSubmitButtonEnabled(true);
return true;
}
以及菜单中的可搜索项:
<item android:id="@+id/search"
android:title="@string/srh_title"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
感谢任何相关的答案!
【问题讨论】:
-
你在哪里打电话给
onSearchRequested()? -
看这个例子可能有帮助:SearchableDictionary
-
我必须在发布此问题后不久解决问题,但还是谢谢你。那是因为我没有在我的主要活动中添加默认的可搜索元数据。我的程序与示例有点不同,而我的程序将搜索功能和主要活动分为两类。并且,onSearchRequested() 函数将使嵌入栏的可搜索文本编辑作为一个独立的视图单独显示。
-
为什么需要 onSearchRequested()?
标签: android search searchable