我正在尝试使用Async任务在编辑文本中更改文本输入时执行搜索服务,如下所示:
et_search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//my asyncTask
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
但是我想停止当前运行异步以在更改文本
上执行新的异步答案 0 :(得分:2)
您可以取消运行 aynctask 首先将aynctask类初始化为全局
private YourAsyncTask mTask;
并在您的editetxt中首先检查您的异步类是否为null而不是先前取消类并再次执行
et_search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//my asyncTask
if(mtask != null){
mTask.cancel(true);
}
mTask = new YourAsyncTask().execute(et_search.getText().toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
您可以在 onTextChanged()方法中调用异步类