简体   繁体   中英

How I change the colors of my buttons between one array colors?

I'm trying change colors of my buttons from one array colors pre-defined. Like this:

  String[] colors = {"#2962ff", "#00bfa5", "#ff6d00", "#aa00ff"};
        Random random = new Random();
        String myRandString = random.nextInt(colors.length);

        // My Buttons
        inGridLayout00.setBackgroundColor(myRandString);
        inGridLayout01.setBackgroundColor(myRandString);
        inGridLayout10.setBackgroundColor(myRandString);
        inGridLayout11.setBackgroundColor(myRandString);

But I don't know how fix this.

You are doing great however you need to pass the random number back to array and get the color for that random index number

  int[] colors = {#2962ff, #00bfa5, #ff6d00, #aa00ff}; Random random = new Random(); int myRandString = random.nextInt(colors.length); // My Buttons inGridLayout00.setBackgroundColor(Color.parseColor(colors[myRandString])); inGridLayout01.setBackgroundColor(Color.parseColor(colors[myRandString])); inGridLayout10.setBackgroundColor(Color.parseColor(colors[myRandString])); inGridLayout11.setBackgroundColor(Color.parseColor(colors[myRandString])); 

All you have to do is

setBackgroundColor(colors[myRandString]);

which gets the color for the random index number

In addition to Siyavash answer.. You should change this part

String myRandString = random.nextInt(colors.length);

to

int myRandString = random.nextInt(colors.length);

And also

   inGridLayout00.setBackgroundColor(colors[myRandString]);

to

inGridLayout00.setBackgroundColor(Color.parseColor(colors[myRandString]));

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