简体   繁体   中英

Android - change color of a button for a certain time

Some expert tell me why this code doesn't work, the purpose is simple to change the color of a button for a certain period of time, after that period you must color returns to original color. The problem is that is not run in order, should be a button to change the color, wait 1s, back to original color then the same for the next button to complete the sequence.

Sorry my bad English and thanks in advance.

    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;
        }
    }
}

You can use message queue:

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;
                }

            }
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM