I want to change the color of the button after 1 second of the button taken out of the list.

Asked 2 years ago, Updated 2 years ago, 41 views

We take them one by one from the list containing the numbers in the code below and repeat them.However, this code will cause the buttons to change color at the same time.

When you run debugging and step-in, it's definitely an iterative process, but I had no idea why this happened.

In the previous question, I used Thread.sleep() to wait, but the color of the button changed after all the for statements were processed.

What I've tried so far
①Handlers: Colors change simultaneously when using handlers
②timer.schedule(): The result of the schedule is the same and the color changes at the same time
③Thread.sleep(): Thread.sleep() ends the for statement and changes color
④I have an idea to timer the entire switch statement, but I can't get the local variable.
⑤I thought I should write a for statement in the run() method to access it locally, but it seems to be against syntax rules

"I just want to say ""Change color → stop for 1 second ->Change color again""…

"

Thank you for your cooperation.

 for (inter:randomList) {
    switch(r){
        case1:
            button1.setBackgroundColor (Color.BLACK);
            newHandler().postDelayed(newRunnable(){
                @ Override
                public void run() {
                    button1.setBackgroundColor (Color.WHITE);
                }
            }, 100);
            break;
        case2:
            button2.setBackgroundColor (Color.BLACK);
            newHandler().postDelayed(newRunnable(){
                @ Override
                public void run() {
                    button2.setBackgroundColor (Color.WHITE);
                }
            }, 100);
            break;
        case3:
            button3.setBackgroundColor (Color.BLACK);
            newHandler().postDelayed(newRunnable(){
                @ Override
                public void run() {
                    button3.setBackgroundColor (Color.WHITE);
                }
            }, 100);
            break;
        case4:
            button4.setBackgroundColor (Color.BLACK);
            newHandler().postDelayed(newRunnable(){
                @ Override
                public void run() {
                    button4.setBackgroundColor (Color.WHITE);
                }
            }, 100);
            break;
    }
}

android java

2022-09-30 19:56

2 Answers

Resolved with comments (posted by the questioner himself)

 for (inter:randomList) {
                    newHandler().postDelayed(newRunnable(){
                        public void run() {

                        }
                    }, listCount3);

                    switch(r){
                        case1:
                            newHandler().postDelayed(newRunnable(){
                                public void run() {
                                    button1.setBackgroundColor (Color.BLACK);
                                }
                            }, listCount);
                            newHandler().postDelayed(newRunnable(){
                                public void run() {
                                    button1.setBackgroundColor (Color.WHITE);
                                }
                            }, listCount2);
                            break;
                        case2:
                            newHandler().postDelayed(newRunnable(){
                                public void run() {
                                    button2.setBackgroundColor (Color.BLACK);
                                }
                            }, listCount);
                            newHandler().postDelayed(newRunnable(){
                                public void run() {
                                    button2.setBackgroundColor (Color.WHITE);
                                }
                            }, listCount2);
                            break;

                    }
                    listCount+=500; listCount2+=500; listCount3+=500;
                }

p Declare 3 fields for postDelayed() method wait time
②By shifting all the wait times in the postDelayed() method, the buttons change color sequentially.

I have solved it, but please let me know if there is any other way.
With C#, it was easy to reproduce by using await.I wish there were some java that correspond to await...I wish there were some easy-to-use methods like Thread.Sleep().


2022-09-30 19:56

It's not wait, but how about creating a thread and Thread.sleep()

private void stepTest(){
        final int[] randomList = new int[] {1,3,2,4};
        final Handler handler = new Handler();

        final Button Button Button1 = findViewById (R.id.button1);
        final Button Button 2 = findViewById (R.id.button2);
        final Button Button 3 = findViewById (R.id.button3);
        final Button Button 4 = findViewById (R.id.button4);

        Runnable = new Runnable() {
            @ Override
            public void run() {
                try{
                    for (inter:randomList) {
                        switch(r){
                        case1:
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button1.setBackgroundColor (Color.BLACK);
                                }
                            });
                            Thread.sleep (1000);
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button1.setBackgroundColor (Color.WHITE);
                                }
                            });
                            break;

                        case2:
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button2.setBackgroundColor (Color.BLACK);
                                }
                            });
                            Thread.sleep (1000);
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button2.setBackgroundColor (Color.WHITE);
                                }
                            });
                            break;

                        case3:
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button3.setBackgroundColor (Color.BLACK);
                                }
                            });
                            Thread.sleep (1000);
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button3.setBackgroundColor (Color.WHITE);
                                }
                            });
                            break;

                        case4:
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button4.setBackgroundColor (Color.BLACK);
                                }
                            });
                            Thread.sleep (1000);
                            handler.post(new Runnable(){
                                @ Override
                                public void run() {
                                    button4.setBackgroundColor (Color.WHITE);
                                }
                            });
                            break;
                        }
                    }
                } catch(InterruptedExceptione) {
                    android.util.Log.e("StepTest", "interrupted", e);
                }
            }
        };

        Thread thr = new Thread (runnable);
        thr.start();
    }

However, this sample does not have the following actions:

  • Do thr.join() somewhere around the end of the process
  • If you want to abort, do thr.interrupt()


2022-09-30 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.