be processed after the loop of the for statement ends

Asked 2 years ago, Updated 2 years ago, 142 views

I am programming on Android Studio.From the list containing random numbers, take the value with a for statement and pass the action with a switch statement.

There is a part called button.setBackgroundColor (Color.Black).
For some reason, the color changes after all the for statements are processed.Please tell me the cause.Thank you for your cooperation.

 for (inter:randomList) {
    try{
        sleep(500);
    } catch(InterruptedExceptione) {
        e.printStackTrace();
    }
    switch(r){
        case1:
            button1.setBackgroundColor (Color.BLACK);
            break;
        case2:
            button2.setBackgroundColor (Color.BLACK);
            break;
        case3:
            button3.setBackgroundColor (Color.BLACK);
            break;
        case4:
            button4.setBackgroundColor (Color.BLACK);
            break;
    }
}

java android

2022-09-30 21:34

1 Answers

I haven't read the source code, so I guess it's because when I call the button.setBackgroundColor() method, the number representing the background color is passed to the button instance, and the actual button background color is not changed.This page may be helpful if you want to set the background color after a while.


2022-09-30 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.