我可以在android中创建SearchActivity
。效果很好,但我想在Query
开始时在Search Widget
中显示SearchActivity
。我怎么能得到这个?
这是我的MainActivity
搜索查询plumber
现在我想要的是,当SearchActivity
被打开时,在输入查询时,plumber
中的Search.Widget
就像MainActivity
一样{。}}。
答案 0 :(得分:0)
这就是你要找的东西 menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.home.activities.TransactionSearchActivity">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
</menu>
private EditText searchPlate;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
if (searchItem != null) {
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
return false;
}
});
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
searchPlate = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchPlate.setHint("Search");
View searchPlateView = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
searchPlateView.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
// use this method for search process
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// use this method when query submitted
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// use this method for auto complete search process
return false;
}
});
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
然后在onResume()
使用此
searchPlate.setText("My Search Query");