Delay call to onQueryTextChange() in SearchView.OnQueryTextListener with SearchView

前端 未结 5 1356
闹比i
闹比i 2021-02-07 05:12

Using a SearchView in my application.

Is there anyway in which I can make the call to onQueryTextChange() method delayed. Like, when user type a sequence o

5条回答
  •  面向向阳花
    2021-02-07 05:53

    Delay call to onQueryTextChange() in SearchView.OnQueryTextListener with SearchView

    private long delay = 2000; 
    private long editTextLastWord = 0;
    privaste  SearchView searchView;
    Handler handler = new Handler();
    
    private Runnable runnable = new Runnable() {
        public void run() {
            if (System.currentTimeMillis() > (editTextLastWord + delay - 500)) {
    
              perFormTask();
            }
        }
    };
     searchView=(SearchView) findViewById(R.id.searchView);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
     if (s.length() > 2) {              
              editTextLastWord = System.currentTimeMillis();
              handler.postDelayed(runnable, delay);
            } else {
    
            }
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
      handler.removeCallbacks(runnable);
                }
            });
    

提交回复
热议问题