Android的SearchView.OnQueryTextListener OnQueryTextSubmit没有开枪空的查询字符串 [英] Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string

查看:970
本文介绍了Android的SearchView.OnQueryTextListener OnQueryTextSubmit没有开枪空的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是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.

这篇关于Android的SearchView.OnQueryTextListener OnQueryTextSubmit没有开枪空的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆