Searchview 在几个字母或几秒钟后自动提交 [英] Searchview auto submit after couple of letters or seconds

查看:36
本文介绍了Searchview 在几个字母或几秒钟后自动提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 android.support.v7.widget.SearchView 我像这样使用:

I have a android.support.v7.widget.SearchView that I use like this:

if (query == null) {
    // Associate searchable configuration with the SearchView
   SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
   SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

   searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
       @Override
       public boolean onQueryTextSubmit(String s) {
           return false;
       }

       @Override
       public boolean onQueryTextChange(String s) {
           Log.e(TAG, "onQueryTextChange(" + s + ")");
           if (!TextUtils.isEmpty(s) && s.length() >= 2) {
               return false;
           }
           return true;
       }
   });
   searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
   searchView.setIconifiedByDefault(false);

我想要的是,当用户写了 2 个字母或 2 秒后,searchview 会自动提交查询.现在我的覆盖方法什么也不做.

What I want is that when the user has written 2 letters or after 2 seconds, the searchview does an auto submit of the query. Now my override methods don't do anything.

我只在第一次点击搜索栏时输入 onQueryTextChanged.不是在添加一些文本之后.但是,当我提交查询然后再次执行时,我会在 onQueryTextChange 中获取所有内容,但又不会自动提交.

I only enter the onQueryTextChanged when I first click on the searchbar. Not after adding some text. However when I submit the query and then do it again, then I get everything in my onQueryTextChange but then again it doesn't auto submit.

有人可以帮助我吗?

推荐答案

使用 editText 而不是 searchView 并添加一个类似这样的 textWatcher:

Use a editText instead of the searchView and add a textWatcher similar to this:

searchView.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // Insert your code here for submitting

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });

这篇关于Searchview 在几个字母或几秒钟后自动提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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