开发者

is it possible to slowdown reaction of edittext listener

i am using an editText listener in my application.Now suppose i am typing something in the editBox its reacting to each and every keyhits as expected ,but i want it to wait it until i complete my text that i am typing ex : i typed www.goog preety fast then i halted for about .5 sec . i want this .5 sec delay to trigger the an editText Listener for the text "www.goog" only one time instead each and every letter . Is it possible.Hope i am clear enough ..plss help

editText.addTextChangedListener(new TextWatcher() {
       public void afterTextChanged(Editable s) {
           String searchText =editText.getText().toString();


            Toast.makeText(getApplicationContext(), searchTe开发者_StackOverflowxt,
                    Toast.LENGTH_SHORT).show();

               //XXX do something
       }
       public void beforeTextChanged(CharSequence s, int start, int count,
               int after) {
               //XXX do something
       }
       public void onTextChanged(CharSequence s, int start, int before, int count) {


       }
   }); 


I couldn't find anything to do the job, so created this:

import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;


public abstract class DelayedTextWatcher implements TextWatcher {

private long delayTime;
private WaitTask lastWaitTask;

public DelayedTextWatcher(long delayTime) {
    super();
    this.delayTime = delayTime;
}

@Override
public void afterTextChanged(Editable s) {
    synchronized (this) {
        if (lastWaitTask != null){
            lastWaitTask.cancel(true);
        }
        lastWaitTask = new WaitTask();
        lastWaitTask.execute(s);
    }
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

public abstract void afterTextChangedDelayed(Editable s);

private class WaitTask extends AsyncTask<Editable, Void, Editable> {

    @Override
    protected Editable doInBackground(Editable... params) {
        try {
            Thread.sleep(delayTime);
        } catch (InterruptedException e) {
        }
        return params[0];
    }

    @Override
    protected void onPostExecute(Editable result) {
        super.onPostExecute(result);
        afterTextChangedDelayed(result);
    }
}

}

When you want to use it do this:

    editText.addTextChangedListener(new DelayedTextWatcher(2000) {

        @Override
        public void afterTextChangedDelayed(Editable s) {
            log.debug("afterTextChangedDelayed >>>>>>>>>>>>> "+s.toString());

        }
    });


You could add an instance variable holding the timestamp of the last time afterTextChange was fired. You can then test in afterTextChange if currentTime - lastTimeTextChanged is above said threshold. If it is, execute code, if not, then not ;)

Something like this:

public void afterTextChanged(Editable s) {

  long currentTime = System.currentTimeMillis();
  if ((currentTime - lastTimeTextChanged) > 500 ) {    
    String searchText =editText.getText().toString();               
    Toast.makeText(getApplicationContext(), searchText, Toast.LENGTH_SHORT).show();                 
    //XXX do something
  }
  lastTimeTextChanged = currentTime;        
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜