gpt4 book ai didi

Android:如何在用户暂停输入时向 Google API 发送请求?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:19 25 4
gpt4 key购买 nike

我正在开发这个应用程序,用户可以在其中在 autoCompleteTextView 中输入位置,它会根据描述的 Google Places Api 建议位置 here .

但是我想通过仅在用户停止输入一定时间后才发送请求来限制应用程序发送的请求量。有谁知道如何做到这一点?

最佳答案

类似于this answer但稍微更简洁并且没有引入额外的状态。您也不需要阈值检查,因为 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);
}
}

关于Android:如何在用户暂停输入时向 Google API 发送请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13193990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com