gpt4 book ai didi

android - 自动完成延迟

转载 作者:太空狗 更新时间:2023-10-29 15:36:20 26 4
gpt4 key购买 nike

我必须为我的应用程序设置自动完成功能。

我已经了解了 AutoCompleteTextView 操作,但我想动态修改 android 自动完成使用的 String[]

我想做的事:调用一个 PHP 页面,它会给我一个 String[],我将在我的 AutoCompleteTextView 中使用它,但我只想在按键至少按下 500 次时这样做最后一个之后的毫秒数。


编辑:

好吧,我错了。如果在最后一次按下后的 500 毫秒内没有按下任何键,我想让我的异步任务运行(你知道,通过对输入的每个字符调用请求来避免对我们的服务器过度收费。)


这是我认为我会做的:

zipBox.setAdapter(myAdapter); //zipBox is an AutoCompleteTextView
zipBox.addTextChangedListener(new TextWatcher(){

@Override
public void onTextChanged(CharSequence s,
int start, int before, int count){

d = new Date();
GetAutocompletion ac = new GetAutocompletion();
ac.execute(zipBox.getText().toString());
}
// Other methods to implement
});


private class GetAutocompletion extends AsyncTask<String, String, String>{

@Override
protected String doInBackground(String... params){

//adapter.notifyDataSetChanged();

try{
wait(500);
} catch(InterruptedException e){}

Date d1 = new Date();

if(d1.getTime() - d.getTime() > 500){
String res = "";

try{

URLConnection conn = new URL("myUrl.php?term=" + params[0]).openConnection();
InputStream in = conn.getInputStream();
Scanner s = new Scanner(in).useDelimiter("\\A");

while (s.hasNext())
res += s.next();

s.close();
in.close();

} catch(Exception e){ Log.d("eduine", "error during autocompletion receive"); }

return json;
} else return null;
}

@Override
protected void onPostExecute(String result){

super.onPostExecute(result);

Log.d("eduine", "json result : " + result);
}

}

你怎么看?无论如何我可以使用 Timer 类吗?

最佳答案

我会保留一个名为 lastPress 的长字段作为我的 TextWatcher 上的一个字段。当您按下一个键时,设置 lastPress = System.currentTimeMillis()。然后只需将整个 onTextChanged 包装在条件为 if(System.currentTimeMillis() - lastPress>500) 的 if 中,然后在该 if 中再次设置 lastPress。


new TextWatcher(){
long lastPress = 0l;
@Override
public void onTextChanged(CharSequence s,
int start, int before, int count){
if(System.currentTimeMillis() - lastpress > 500){
lastPress= System.currentTimeMillis();
GetAutocompletion ac = new GetAutocompletion();
ac.execute(zipBox.getText().toString());
}
}
// Other methods to implement
}

关于android - 自动完成延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20761113/

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