我想在动态更改不同BUTTONS的背景图像时引入5秒延迟,您可以在下面看到我的代码。我测试了数字内置的延迟方法,即SLEEP(),WAIT()等,但它会让我的应用程序进入睡眠状态。
请有人帮我开发“myDelayMethod(mSeconds);”
我会非常感谢你。
int mSeconds =5;//Seconds
for(number=1; number<5;number++)
{
if(number==1)
{
buttonLED1.setBackgroundResource(R.drawable.buttonyellow);
myDelayMethod(mSeconds);
buttonLED1.setBackgroundResource(R.drawable.buttonblue);
myDelayMethod(mSeconds);
}
else if(number==2)
{
buttonLED2.setBackgroundResource(R.drawable.buttonyellow);
myDelayMethod(mSeconds);
buttonLED2.setBackgroundResource(R.drawable.buttonblue);
myDelayMethod(mSeconds);
}
else if(number==3)
{
buttonLED3.setBackgroundResource(R.drawable.buttonyellow);
myDelayMethod(mSeconds);
buttonLED3.setBackgroundResource(R.drawable.buttonblue);
myDelayMethod(mSeconds);
}
else if(number==4)
{
buttonLED4.setBackgroundResource(R.drawable.buttonyellow);
myDelayMethod(mSeconds);
buttonLED4.setBackgroundResource(R.drawable.buttonblue);
myDelayMethod(mSeconds);
}
}
答案 0 :(得分:1)
Button[] buttons = {buttonLED1, buttonLED2, buttonLED3, buttonLED4};
for (Button button : buttons) {
button.postDelayed(new Runnable() {
@Override
public void run() {
button.setBackgroundResource(R.drawable.buttonblue);
}
}, 1000 * mSeconds);
}
答案 1 :(得分:0)
您可能想要查找Handler.postDelayed()
。