1

我想在动态更改不同按钮的背景图像时引入 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);
    }
}
4

2 回答 2

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);
}
于 2012-09-27T15:59:28.547 回答
0

您可能想要寻找Handler.postDelayed().

于 2012-09-27T15:33:52.087 回答