在发布此消息之前,我尝试了所有可能的解决方案。最后我陷入了困境,没有运气。我只想在搜索活动中显示查询字符串。有帮助吗?提前致谢。
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/my_app"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="android.app.default_searchable"
android:value="com.mycompany.mygoodapp.SearchResultsActivity"/>
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/my_app"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchResultsActivity"
android:label="@string/my_app"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
SearchResultsActivity实现如下:
public class SearchResultsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
DisplayToast("Here ...");
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
DisplayToast(query);
}
}
public void DisplayToast(String s) {
Toast.makeText(SearchResultsActivity.this,
s, Toast.LENGTH_LONG).show();
}
}
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/my_app"
android:hint="@string/search_hint" />
MainActivity:
@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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
ComponentName cn = new ComponentName("com.mycompany.mygoodapp", "com.mycompany.mygoodapp.SearchResultsActivity");
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
searchView.setIconifiedByDefault(true);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
///
else if(id==R.id.search)
{
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
主菜单布局搜索菜单项:
<item
android:id="@+id/search"
android:title="@string/search_title"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView">
答案 0 :(得分:0)
您可以通过双向搜索活动
1种方式
菜单/ options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/disha"
app:showAsAction="collapseActionView|always"
android:title="search"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
您活动的java代码
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
在最明确的文件中添加
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
<强> searchable.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 hint" />
使用自定义操作栏的方式 或者您也可以尝试
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setCustomView(R.layout.search_layout);
search = (EditText) getSupportActionBar().getCustomView().
findViewById(R.id.searchfield);
search.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
return true;
}
});
getSupportActionBar().setDisplayOptions(getSupportActionBar().DISPLAY_SHOW_CUSTOM
| getSupportActionBar().DISPLAY_SHOW_HOME);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
比创建这样的自定义actiob栏布局
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchfield"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_search_edit"
android:hint="@string/search_hint"
android:inputType="textFilter"
android:textColor="@color/colorBlack"
android:textColorHint="@color/colorAccent"
android:textSize="15sp" />
尝试这个并在遇到任何疑问时问我