当用户在ActionBar
的搜索小工具中提交应用范围内的搜索时,我希望它在提交后自动崩溃。提交基本上会打开SearchableActivity
。目前,如果他们关闭搜索结果活动(SearchableActivity
),则搜索窗口小部件仍处于打开状态。我只是希望它关闭;没有文字。
从阅读中,我似乎认为答案就是这个
`searchView.onActionViewCollapsed() ;'
然而,我不知道该把它放在哪里?
这是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setupNewSearchView(searchItem, searchManager);
}
return true;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupNewSearchView(final MenuItem searchItem,
SearchManager searchManager) {
android.widget.SearchView searchView = (android.widget.SearchView) searchItem
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem About = menu.findItem(R.id.About);
MenuItem Login = menu.findItem(R.id.Login);
MenuItem Logout = menu.findItem(R.id.Logout);
MenuItem Settings = menu.findItem(R.id.Settings);
MenuItem Search = menu.findItem(R.id.menu_search);
MenuItem Add = menu.findItem(R.id.Add);
Add.setVisible(false);
if (LoggedStatus == true) {
// show the log out option
Logout.setVisible(true);
Login.setVisible(false);
Settings.setVisible(true);
} else {
// show the log in option
Logout.setVisible(false);
Login.setVisible(true);
Settings.setVisible(false);
}
About.setVisible(true);
return true;
}
注意,我使用的是SherlockActionBar
。
答案 0 :(得分:6)
我这样做了:
searchView.setOnQueryTextListener(new OnQueryTextListener() {
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onQueryTextSubmit(String arg0) {
if (searchItem != null) {
searchItem.collapseActionView();
}
return false;
}
});
并在我的xml菜单文件中:
android:showAsAction="always|collapseActionView"