我在下面使用Android自动填充功能库是我的代码:
private void callGoogleAutoComplete(String newText) {
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setCountry("PK")
.build();
Task<AutocompletePredictionBufferResponse> results=mGeoDataClient.getAutocompletePredictions(newText,myBounds,typeFilter);
results.addOnCompleteListener(new OnCompleteListener<AutocompletePredictionBufferResponse>() {
@Override
public void onComplete(@NonNull Task<AutocompletePredictionBufferResponse> task) {
try {
String response="";
} catch (Exception e) {
e.printStackTrace();
}
}
});
results.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
results.addOnSuccessListener(new OnSuccessListener<AutocompletePredictionBufferResponse>() {
@Override
public void onSuccess(AutocompletePredictionBufferResponse autocompletePredictions) {
}
});
}
每次在EditText中更改文本时,都会调用上面的方法,我想在生成每个角色任务时取消过去的调用(Task)。但我无法取消任务。
注意
我只为听众发布了必要的代码。
答案 0 :(得分:0)
我认为这是延迟用户输入的更好方法。等待用户完成输入然后调用您的方法它可以帮助您。像这样
editText.addTextChangedListener(
new TextWatcher() {
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
private Timer timer=new Timer();
private final long DELAY = 1000; // milliseconds
@Override
public void afterTextChanged(final Editable s) {
timer.cancel();
timer = new Timer();
timer.schedule(
new TimerTask() {
@Override
public void run() {
// TODO: do what you need here (refresh list)
// you will probably need to use runOnUiThread(Runnable action) for some specific actions
}
},
DELAY
);
}
}
);