示例2:使用postDelayed方法 近期本站新介绍的特性中,我每次都要模拟EditText的自动完成功能,每次文字改变后都会触发一个API的调用,从服务器中检索数据。 我想减少APP调用API的次数,所以决定使用Handler的postDelayed方法来实现这个功能。 本例不针对平行处理,只是关于Handler给消息队列发送消息还有安排消息在未来的某一点执行等。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | private long lastChange = 0 ;
@Override
public void onTextChanged( final CharSequence chars,
int start, int before, int count) {
new Handler().postDelayed(
new Runnable() {
@Override
public void run() {
if (noChangeInText_InTheLastFewSeconds()) {
searchAndPopulateListView(chars.toString());
}
}
},
300 );
lastChange = System.currentTimeMillis();
}
private boolean noChangeInText_InTheLastFewSeconds() {
return System.currentTimeMillis() - lastChange >= 300
}
|
最后我就把“postAtTime”这个方法作为联系留给读者们了,掌握Handler了吗?如果是的话,那么可以尽情使用线程了。
原文链接: weddingpartyapp 翻译: 伯乐在线 - chris 译文链接: http://blog.jobbole.com/73267/ |