是否有可能减慢edittext监听器的反应速度

时间:2011-04-20 12:51:35

标签: android

我正在我的应用程序中使用editText侦听器。现在假设我正在输入内容 在editBox中它按预期对每个键盘作出反应,但我希望它等到我完成我正在键入的文本   例如:我快速输入www.goog preety然后我停止了大约0.5秒。我想要.5秒延迟来触发文本“www.goog”的editText Listener,而不是每一个字母。是否有可能。希望我足够清楚..plss help

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


            Toast.makeText(getApplicationContext(), searchText,
                    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) {


       }
   }); 

2 个答案:

答案 0 :(得分:11)

我找不到任何工作,所以创造了这个:

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

}

如果您想使用它,请执行以下操作:

    editText.addTextChangedListener(new DelayedTextWatcher(2000) {

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

        }
    });

答案 1 :(得分:5)

您可以添加一个实例变量,其中包含上次afterTextChange被触发的时间戳。然后,如果afterTextChange高于上述阈值,您可以在currentTime - lastTimeTextChanged中进行测试。如果是,则执行代码,否则执行代码,然后执行代码;)

这样的事情:

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