简体   繁体   中英

Android: Change Button Colors in a Pattern

I'm trying to implement something like Simon Says for Android. This is the code I have:

for(int i = 1; i <= sequence.size(); i++){
handler.postDelayed(new Runnable(){
                    public void run(){
                        if (sequence.peek() == 1){
                            green.setPressed(true);
                            handler.postDelayed(new Runnable(){
                                public void run(){
                                    green.setPressed(false);                
                                    }
                            }, 1000);
                        }
                        else if (sequence.peek() == 2){
                            red.setPressed(true);
                            handler.postDelayed(new Runnable(){
                                public void run(){
                                    red.setPressed(false);               
                                    }
                            }, 1000);
                        }
                        else if (sequence.peek() == 3){
                            yellow.setPressed(true);
                            handler.postDelayed(new Runnable(){
                                public void run(){
                                    yellow.setPressed(false);               
                                    }
                            }, 1000);
                        }
                        else if (sequence.peek() == 3){
                            blue.setPressed(true);
                            handler.postDelayed(new Runnable(){
                                public void run(){
                                    blue.setPressed(false);             
                                    }
                            }, 1000);
                        }
                        int myNum = sequence.peek();
                        sequence.poll();
                        sequence.add(myNum);
                    }
                }, 1000);
            }
}

Basically, I have a queue (sequence) that holds the pattern to blink button colors. (setPressed(true) is a different color than setPressed(false)) The first time it is called, there is only one button that needs to change colors so it works fine. The second time it is called, 2 button colors should change. My code right now will light both up at the same time. How do I make it so that it will blink one button color first, finish that, and then blink the second button color? I've tried a million different things and this is still the closest I've gotten.

My guess would be, without having tested any code, that it's because of your for loop. Basically, it runs through all the sequences, and post's the runnable to the handler with a delay of 1000 milliseconds.

That means: i is 0: sequence1 starts in 1000 milliseconds; i is 1: sequence1 starts in 1000 milliseconds as well; i is 2....

Because it's in a handler, it does not affect the runtime of the for loop, which means all iterations are passed directly after each other, which results in every runnable is executed at "almost" the same time.

Just try to set the delay to (i+1)*1000, so that each "button" will be activated (for a second) when it's "his time".

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