在网上搜索答案如何实现搜索选项并将其链接到WebView时,我找到了一个例子,但是执行搜索时出现问题,然后启动第二个Activity,我该如何搜索没有初始化新活动的网页?
示例代码:
\ AndroidManifest.xml中
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
\ main.xml中
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search"
android:title="@string/menu_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
</menu>
\ searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable android:label="@string/search"
android:hint="@string/search_hint"
android:searchSuggestAuthority=""
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData=""
xmlns:android="http://schemas.android.com/apk/res/android" />
\ MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Intent searchIntent = getIntent();
String query = searchIntent.getStringExtra(SearchManager.QUERY);
if(Intent.ACTION_SEARCH.equals(searchIntent.getAction())) {
loadPage("https://sayt.com//index.php?do="+query);
}
return super.onCreateOptionsMenu(menu);
}