Searchview会在几个字母或秒后自动提交

时间:2014-05-28 12:14:03

标签: android searchview

我有android.support.v7.widget.SearchView,我这样使用:

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秒后,搜索视图会自动提交查询。现在我的覆盖方法什么都不做。

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

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

使用editText而不是searchView并添加类似于此的textWatcher:

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
        }
    });

答案 1 :(得分:1)

使用此代码,我遇到了同样的问题并想出了如何实现它。

代码被评论,我相信它是自我解释的,否则随意提出任何问题!

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    MenuItem searchBackupsButton = menu.findItem(R.id.action_search);
    searchView = (SearchView) searchBackupsButton.getActionView();

    // Set menu items visible
    searchBackupsButton.setVisible(true);
    setupSearchView(searchBackupsButton);
}



private void setupSearchView(final MenuItem searchItem) 
{
    if (isAlwaysExpanded()) 
    {
        searchView.setIconifiedByDefault(false);
    } 
    else 
    {
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query)
        {
            // If you are using a pager view (tabbed) you can get the current one here
            if(pager.getCurrentItem() == 0)
            {
                // do your searches here

                // Hide the keyboard
                searchView.clearFocus();

            }
            // same as above
            else if(pager.getCurrentItem() == 1)
            {
                // do your searches here

                // Hide the keyboard
                searchView.clearFocus();
            }
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) 
        {
            // Here for each key pressed there will be a timer starting from X and drecrementing.
            // When the timer reaches zero it will perform a search
            // So, lets say the user inserted letter 'a'. 500ms later, if the user didnt input
            // anything else the search for query 'a' will be made.
            Log.d(TAG, "Query: " + newText);

            // if there is already a count down running, cancel it and start a new one
            if(isCountDownTimerRunning)
            {
                countDownTimer.cancel();
            }

            // There is nothing to search for, show the full list!
            // This is usefull when the user presses the cross 'x' on the search view
            // Then the full list will be shown again
            // Or when the user presses backspace until it clears the searchview
            if("".equals(newText) || newText == null)
            {
                // check what current fragment is on the pager
                if(pager.getCurrentItem() == 0)
                {
                    // There is nothing to search for, show the full list!
                }
                // if cloud backups
                else if(pager.getCurrentItem() == CLOUD_BACKUPS_FRAGMENT_POSITION)
                {
                    // There is nothing to search for, show the full list!
                }
            }
            else
            {
                // Get the active fragment
                if(pager.getCurrentItem() == 0) 
                    countDownTimer = timedSearchQuery(TIME_INTERVAL_BETWEEN_KEY_PRESSED_AND_SEARCH, newText, 0);
                else if(pager.getCurrentItem() == 1)
                    countDownTimer = timedSearchQuery(TIME_INTERVAL_BETWEEN_KEY_PRESSED_AND_SEARCH, newText, 1);
            }
            return false;
        }
    });
}

    private CountDownTimer timedSearchQuery(long timeInMillis, final String searchFor, final int fragment)
{           
    countDownTimer = new CountDownTimer(timeInMillis, timeInMillis/2) { 
        @Override
        public void onTick(long millisUntilFinished) {
        }
        @Override
        public void onFinish() {
            if(fragment == 0)
                // do your search on fragment @ position zero;
            else if(fragment == 1)
                // do your search on fragment  2 position 1

                // and so on...
        }
    };

    countDownTimer.start();
    isCountDownTimerRunning = true;

    return countDownTimer;
}

protected boolean isAlwaysExpanded() {
    return false;
}