我正在使用它进行软件搜索(当用户按下放大镜时在菜单中折叠搜索)并且它完美地运行:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_entries, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.viewMenu_search).getActionView();
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.YELLOW);
textView.setHintTextColor(Color.WHITE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return true;
}
@Override
public boolean onQueryTextChange(String s) {
ViewEntries.this.customAdapter.getFilter().filter(s);
return true;
}
});
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
我的问题是:当用户使用硬件按钮开始搜索时,如何获取搜索查询字符串(类似参数String s)。我试过这个,但我得到的意图不是搜索意图,而是启动活动的那个(所以我从不输入if):
@Override
public boolean onSearchRequested() {
Intent intent1 = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String s = intent1.getStringExtra(SearchManager.QUERY);
ViewEntries.this.customAdapter.getFilter().filter(s);
}
return super.onSearchRequested();
}
我还说当在模拟器上调试时,在您开始搜索的那一刻调用onSearchRequested,而不是在您输入查询之后。也许暗示如何获得searchview(id或者其他)?