【发布时间】:2023-04-03 06:26:02
【问题描述】:
我想知道如何在 android studio 的 for 循环中添加延迟。我想将按钮的背景更改为某种颜色,然后将其更改回来。然后我想继续循环的下一次迭代。
我尝试使用处理程序和倒数计时器,但没有得到预期的效果。循环不会暂停和等待。
这就是我真正想做的:
Random rand = new Random();
int [] pattern = new int[score + 1];
for (int j = 0; j < score+1; j++) {
pattern[j] = rand.nextInt(4);
//Here what I want is depending on the number,
I will change the colour and wait for a second then change it back all before the next iteration starts.
}
【问题讨论】:
-
如果问题是 Timer 或 Thread.sleep 将锁定用户界面,那么您可以使用带有重复或 thread.sleep 的警报创建服务,然后每隔 X 秒触发一次广播服务(在服务内),最后收听这个广播(在 MainActivity 或 UI 中)并在此处更改您的颜色
标签: loops android-studio