Okay so I'm trying to stylize this tictactoe game I'm working on for a project. When I press new game, I would like some sort of an array that would change the background of each Button to a color. Then when the user clicks a button to make their move there, that space will change to a different color depending on the player (I have a PlayerTurn flag variable that determines this). I would also like to a value in an int array according to an index that corresponds with that button. How can I assign each button a numerical value that I can later use to index into my array?
Here is the code I'm currently using.
Button buttons[] = new Button[9];
buttons[0] = (Button) findViewById(R.id.button0);
buttons[1] = (Button) findViewById(R.id.button1);
buttons[2] = (Button) findViewById(R.id.button2);
buttons[3] = (Button) findViewById(R.id.button3);
buttons[4] = (Button) findViewById(R.id.button4);
buttons[5] = (Button) findViewById(R.id.button5);
buttons[6] = (Button) findViewById(R.id.button6);
buttons[7] = (Button) findViewById(R.id.button7);
buttons[8] = (Button) findViewById(R.id.button8);
You can create an ArrayList
of the type Button
:
private ArrayList<Button> mList;
onCreate()//
mList = new ArrayList<>();
mList.add(yourButton);
.....................
for(int i = 0 ; i< mList.size(); i++)
mList.get(i).setBackground(//your color);
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.