我的目标是复制“播放音乐”应用搜索功能。从我可以收集的信息中,当我按下搜索按钮时,会创建一个新活动,并自动展开搜索视图。当按下箭头或后退按钮以关闭搜索小部件时,活动将关闭。一切都有效,除了没有被调用的onDismiss方法。任何帮助深表感谢。谢谢!
当按下搜索图标时,搜索活动通过意图启动。您可以在下面看到在搜索活动中创建搜索小部件的代码,该代码模仿播放音乐,如上所述。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
searchMenuItem.expandActionView();
searchManager.setOnDismissListener(new SearchManager.OnDismissListener() {
@Override
public void onDismiss() {
//Method never called. Logs don't show up/
Log.i("1234Dismiss", "onDismiss called");
finish();
}
});
return super.onCreateOptionsMenu(menu);
}
答案 0 :(得分:2)
请改用它。它在我的所有应用程序中都非常适合我。
MenuItemCompat.setOnActionExpandListener(searchMenuItem,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do what you want to do when the search is collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do what you want to do when the search is expanded
return true; // Return true to expand action view
}
});