Java 如何在不使用thread.sleep的情况下延迟android中的循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20896245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delay a loop in android without using thread.sleep?
提问by user3153613
I wanted to delay a for loop without using Thread.sleep
because that method make my whole application hang. I tried to use handler
but it doesn't seems to work inside a loop. Can someone please point out the mistake in my code.
我想在不使用的情况下延迟 for 循环,Thread.sleep
因为该方法使我的整个应用程序挂起。我尝试使用,handler
但它似乎在循环中不起作用。有人可以指出我的代码中的错误。
public void onClick(View v) {
if (v == start)
{
for (int a = 0; a<4 ;a++) {
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
ImageButton[] all= {btn1, btn2, btn3, btn4};
btn5 = all[random.nextInt(all.length)];
btn5.setBackgroundColor(Color.RED);
@Override
public void run() {
}
}, 1000);
}
}
}
Basically what I wanted to do is that I got 4 ImageButton
and I change each of their background to red by using a loop in order. Thats why I need a delay inside my loop, if not all the ImageButton
will just directly turn red without showing which ImageButton
turn first.
基本上我想做的是得到 4 个,ImageButton
然后按顺序使用循环将它们的每个背景更改为红色。这就是为什么我需要在我的循环中延迟,如果不是所有的ImageButton
都会直接变成红色而不显示ImageButton
先转哪个。
采纳答案by sddamico
Your for loop should be:
你的 for 循环应该是:
final ImageButton[] all= {btn1, btn2, btn3, btn4};
Handler handler1 = new Handler();
for (int a = 1; a<=all.length ;a++) {
handler1.postDelayed(new Runnable() {
@Override
public void run() {
ImageButton btn5 = all[random.nextInt(all.length)];
btn5.setBackgroundColor(Color.RED);
}
}, 1000 * a);
}
}
This way it achieves your desired behavior of staggering the color change.
通过这种方式,它可以实现您想要的颜色变化交错的行为。
Edited for syntax
编辑语法
回答by Imran Ali Khan
Try this :
尝试这个 :
public void onClick(View v) {
if (v == start) {
for (int a = 0; a<4 ;a++) {
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
ImageButton[] all= {btn1, btn2, btn3, btn4};
@Override
public void run() {
btn5 = all[random.nextInt(all.length)];
btn5.setBackgroundColor(Color.RED);
}
}, 1000);
}
}
}
Example for Delay :
延迟示例:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
buttons[inew][jnew].setBackgroundColor(Color.Red);
}
}, 5000);
回答by Raghunandan
You can use a Handler
instead of for loop. You should not call Thread.sleep()
on the UI thread.
您可以使用 aHandler
而不是 for 循环。您不应该Thread.sleep()
在 UI 线程上调用。
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// do something
handler.postDelayed(this, 1000L); // 1 second delay
}
};
handler.post(runnable);
回答by Cody
The following code doing the task in every second:
以下代码每秒执行一次任务:
final Handler handler = new Handler();
Runnable task = new Runnable() {
@Override
public void run() {
Log.d(TAG, "Doing task");
handler.postDelayed(this, 1000);
}
};
handler.post(task);