我正在尝试使用异步任务执行搜索服务,以更改编辑文本中的文本输入,如下所示:
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) {
}
});
但我想停止当前运行的异步以在更改文本时执行新的异步
最佳答案
您可以取消在您想要的地方运行aynctask 首先将您的 aynctask 类初始化为globally
private YourAsyncTask mTask;
并且在你的 editetxt 中首先检查你的异步类是否不为空,然后取消之前的类并再次执行
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()方法中调用异步类
关于android - 如何在更改文本输入时使用异步任务执行搜索服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41887063/