安卓系统——在一段时间内改变按钮的颜色



一些专家告诉我为什么这段代码不工作,目的很简单,在一段时间内改变按钮的颜色,在这段时间之后你必须颜色恢复到原来的颜色。问题是,不是按顺序运行,应该是一个按钮改变颜色,等待15秒,回到原来的颜色然后为下一个按钮相同的顺序完成。

对不起,我的英语不好,提前感谢。

    int temp[] = new int[game.getLevel()];
    Handler handler = new Handler(); 
    temp = game.getSequence();
    for(int i = 0; i < game.getLevel(); i++)
    {
        switch (temp[i])
        {
            case RED:
                handler.postDelayed(new Runnable() { 
                    public void run() { 
                              redButton.setBackgroundColor(Color.rgb(255, 0, 0));
                         } 
                    }, 1000); 
                redButton.setBackgroundColor(Color.rgb(109, 0, 0));
                break;
            case GREEN:
                handler.postDelayed(new Runnable() { 
                    public void run() { 
                              greenButton.setBackgroundColor(Color.rgb(0, 255, 0));
                         } 
                    }, 1000); 
                 greenButton.setBackgroundColor(Color.rgb(0, 109, 0));
                break;
            case YELLOW:
                handler.postDelayed(new Runnable() { 
                    public void run() { 
                              yellowButton.setBackgroundColor(Color.rgb(255, 255, 0));
                         } 
                    }, 1000); 
                 yellowButton.setBackgroundColor(Color.rgb(109, 109, 0));
                break;
            case BLUE:
                handler.postDelayed(new Runnable() { 
                    public void run() { 
                              blueButton.setBackgroundColor(Color.rgb(0, 0, 255));
                         } 
                    }, 1000); 
                 blueButton.setBackgroundColor(Color.rgb(0, 0, 255));
                break;
            default:
                break;
        }
    }
}

您可以使用消息队列:

static final int RED = 0;
static final int GREEN = 1;
static final int YELLOW = 2;
static final int BLUE = 3;
mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mHandler.sendEmptyMessageDelayed(RED, 1000);
            }
        });
mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                switch (msg.what) {
                case RED:
                    mButton.setBackgroundColor(Color.rgb(255, 0, 0));
                    mHandler.sendEmptyMessageDelayed(GREEN, 1000);
                    break;
                case GREEN:
                    mButton.setBackgroundColor(Color.rgb(0, 255, 0));
                    mHandler.sendEmptyMessageDelayed(YELLOW, 1000);
                    break;
                case YELLOW:
                    mButton.setBackgroundColor(Color.rgb(255, 255, 0));
                    mHandler.sendEmptyMessageDelayed(BLUE, 1000);
                    break;
                case BLUE:
                    mButton.setBackgroundColor(Color.rgb(0, 0, 255));
                    mHandler.sendEmptyMessageDelayed(RED, 1000);
                    break;
                }
            }
        };