问题描述:

In Android is there a way of setting up a button such that every time I run the program the button has different properties?

i.e. A button having a different colour background every time I run the program

网友答案:

In an activity you find your button with

Button btn = findViewById(R.id.myButtonId);

and set it's background color like this

int color = // generate some random color
btn.setBackgroundColor(color);
网友答案:

You can use Random() in order to set background color to button .

For Example:

  int[] color={Color.RED,Color.BLACK,Color.GREEN};

  onCreate()
  {
    Button b=(Button)findViewById(R.id.button1);

    Random random=new Random();

    b.setBackgroundColor(color[random.nextInt(3)]);

  }
相关阅读:
Top