Android 如何在更改文本输入时使用异步任务执行搜索服务
我正在尝试使用异步任务执行搜索服务,以更改编辑文本中的文本输入,如下所示:Android 如何在更改文本输入时使用异步任务执行搜索服务,android,android-asynctask,Android,Android Asynctask,我正在尝试使用异步任务执行搜索服务,以更改编辑文本中的文本输入,如下所示: et_search.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { //my asyncTask } } public void beforeTextChan
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) {
}
});
但是我想停止当前正在运行的async,以便在更改文本时执行新的任务您可以取消在需要的地方运行aynctask 首先将aynctask类全局初始化为
private YourAsyncTask mTask;
在EditeText中,首先检查异步类是否不为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()方法中调用异步类