我正在开发一个应用程序,用户可以在autoCompleteTextView中输入位置,它将基于谷歌地方API建议位置,如这里所述。
但是我想通过仅在用户停止键入一定时间后发送请求来限制应用程序发送的请求量。有人知道如何做到这一点吗?
但是我想通过仅在用户停止键入一定时间后发送请求来限制应用程序发送的请求量。有人知道如何做到这一点吗?
与此答案类似,但稍微更加简洁,而且不会引入额外的状态。由于只有在实际需要过滤时才会调用performFiltering
,所以您也不需要阈值检查。
似乎只有通过子类化 AutoCompleteTextView
才能实现,因为您不能覆盖/替换由 AutoCompleteTextView
添加的TextWatcher
。
public class DelayAutoCompleteTextView extends AutoCompleteTextView {
public DelayAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
DelayAutoCompleteTextView.super.performFiltering((CharSequence) msg.obj, msg.arg1);
}
};
@Override
protected void performFiltering(CharSequence text, int keyCode) {
mHandler.removeMessages(0);
mHandler.sendMessageDelayed(mHandler.obtainMessage(0, keyCode, 0, text), 750);
}
}
我已经找到了一个解决上述问题的方案,这个方案运作得非常好。我已经通过以下方式扩展了AutoCompleteTextView。如果有人知道如何进一步改进这段代码,请告诉我。
public class MyAutoCompleteTextView extends AutoCompleteTextView {
// initialization
int threshold;
int delay = 750;
Handler handler = new Handler();
Runnable run;
// constructor
public MyAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void performFiltering(final CharSequence text, final int keyCode) {
// get threshold
threshold = this.getThreshold();
// perform filter on null to hide dropdown
doFiltering(null, keyCode);
// stop execution of previous handler
handler.removeCallbacks(run);
// creation of new runnable and prevent filtering of texts which length
// does not meet threshold
run = new Runnable() {
public void run() {
if (text.length() > threshold) {
doFiltering(text, keyCode);
}
}
};
// restart handler
handler.postDelayed(run, delay);
}
// starts the actual filtering
private void doFiltering(CharSequence text, int keyCode) {
super.performFiltering(text, keyCode);
}
}
public class CustomCompleteView extends AutoCompleteTextView{
Handler handler=new Handler();
Runnable r;
boolean cleartogo=false;
boolean pending =false;
CharSequence btext;
int bkeyCode;
public CustomCompleteView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomCompleteView(Context context, AttributeSet attrs)
{
super(context,attrs);
}
public CustomCompleteView(Context context, AttributeSet attrs, int defStyle)
{
super(context,attrs,defStyle);
}
@Override
public void performFiltering(CharSequence text, int keyCode){
if (cleartogo){
cleartogo=false;
Log.d(MainActivity.DTAG,"Going to filter on " + text.toString());
pending=false;
super.performFiltering(btext, bkeyCode);
}
else
{
Log.d(MainActivity.DTAG,"Filtering rejected, too soon");
btext=text;
bkeyCode=keyCode;
if (!pending){
if (r==null)
r=new MyRunnable(this);
//try{handler.removeCallbacks(r);} catch (Exception ex){};
handler.postDelayed(r, 500);
pending=true;}
}
}
private class MyRunnable implements Runnable {
CustomCompleteView bc;
MyRunnable(CustomCompleteView c ) {
this.bc=c;
}
public void run() {
Log.d(MainActivity.DTAG,"Special Runnable running");
cleartogo=true;
bc.performFiltering(btext, bkeyCode);
}
}
}
AutoCompleteTextView
将内部CharSequence
的引用而不是已输入文本的副本传递给您,否则计划的延迟消息和handleMessage()
被调用之间键入的任何字符都将丢失。当handleMessage()
方法调用performFiltering()
时,最安全的方法可能是从View中提取文本。 - Colin M.