【发布时间】:2016-05-20 12:08:57
【问题描述】:
我正在尝试在我的工具栏中实现 SearchView,但是当我输入内容并确认搜索时,它不会打开活动的结果。
搜索出现在 MainActivity 中,当用户键入内容并按下搜索按钮时,它应该打开 SearchResultAcivity。但是打不开。
这是我所做的:
AndroidManifest:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<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>
</activity>
可搜索的.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="search" >
</searchable>
菜单项:
<item
android:id="@+id/action_search"
android:title="Search"
android:orderInCategory="100"
android:icon="@drawable/search"
app:actionViewClass = "android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
还有:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Iconify the widget
return true;
}
【问题讨论】:
标签: searchview