我使用的是Android 4.1.2。我有一个动作条
A搜索查看小部件。文档的 SearchView.OnQueryTextListener
从Android开发者网站指出, onQueryTextSubmit
时叫,当用户提交查询解雇这可能是由于在键盘上或由于pressing提交按钮密钥preSS的。
I am using Android 4.1.2. I have a SearchView widget on an ActionBar
. Documentation on SearchView.OnQueryTextListener
from the android developer site states that onQueryTextSubmit
is fired when "Called when the user submits the query. This could be due to a key press on the keyboard or due to pressing a submit button."
这不会发生,如果搜索查询为空。我需要这个火在一个空的查询清除ListView的搜索过滤器。这是一个错误还是我做错了什么?
This does not happen if the search query is empty. I need this to fire on an empty query to clear the search filter of a ListView. Is this a bug or am I doing something wrong?
我有同样的问题,并最终获得了以下解决方案:自定义搜索查看
+ OnQueryTextListener.onQueryTextChange
I had the same problem and end up with the following solution: custom SearchView
+ OnQueryTextListener.onQueryTextChange
自定义搜索查看:
Custom SearchView:
public class MySearchView extends SearchView {
private boolean expanded;
public MySearchView(Context context) {
super(context);
}
@Override
public void onActionViewExpanded() {
super.onActionViewExpanded();
expanded = true;
}
@Override
public void onActionViewCollapsed() {
super.onActionViewCollapsed();
expanded = false;
}
public boolean isExpanded() {
return expanded;
}
}
创建行动,并设置回调:
Creating action and setting callback:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
searchAction = menu.add(0, SEARCH_ACTION_ID, 0 , getString(R.string.action_search));
searchAction.setShowAsAction(SHOW_AS_ACTION_ALWAYS | SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchView = new MySearchView(getSherlockActivity());
searchView.setOnQueryTextListener(searchQueryListener);
searchView.setIconifiedByDefault(true);
searchAction.setActionView(searchView);
}
和最后听众:
And last the listener:
private OnQueryTextListener searchQueryListener = new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
search(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (searchView.isExpanded() && TextUtils.isEmpty(newText)) {
search("");
}
return true;
}
public void search(String query) {
// reset loader, swap cursor, etc.
}
};
在测试了ABS 4.3。
Tested on ABS 4.3.