使循环延迟一段时间
问题描述:
在这段代码中,我想让for循环延迟700毫秒,我该如何实现它。在这段代码中,循环完成了700毫秒延迟的bez,但是我想让for循环的每次迭代延迟700毫秒。更多的疑问是我不应该在UI线程上使用Thread.sleep()
;使循环延迟一段时间
for(int i=0;i<Uirkeys.length;i++){
Handler handler=new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run() {
copyView.get(cnt1).setBackgroundColor(Color.BLACK);
TeaxtToSpeach(map.get(Uirkeys[cnt1]));
cnt1++;
}
},700);
}
答
请尝试以下代码。
try{
Thread.sleep(1000);
}catch(InterruptedException ex){
//do stuff
}
使用AsyncTask来执行循环。然后你可以使用Thread.sleep() – 2014-10-09 14:09:14