Reputation: 3
I'm making a game as an android app in which the user clicks on buttons to change its color and so on...
One thing I'm trying to implement is to make some initial moves when the app is started by randomly performing clicks on various buttons. However, I'm having a real hard time trying to figure out how to randomly select some number of buttons and do its performClick() method. Does anyone have any ideas?
Thank you Billy
Upvotes: 0
Views: 1673
Reputation: 11
Very Simple way to select Radio Button Randomly: suppose 3 radio buttons are there
int a = new Random().nextInt(3);
if(a == 0)
{
idAccountOption.click();
//(idAccountOption)-id of radio button on application
}
else if(a == 1)
{
idPremisesOption.click();
}
else if(a == 2)
{
idRouteOption.click();
}
Upvotes: 1
Reputation: 748
What Mighter said above should work. But it sounds like the code would be cleaner and more MVC-like if you separate your view code(button handler) from controller(the logic to alter game state), and directly call your controller instead of doing performClick(), in other words:
Move the "change color" logic inside each button click handler into a method alterState(int actionId);
Call alterState() within each button's click handler
When the app starts, call alterState(new Random().nextInt() % NUM_ACTIONS) in a loop to perform your random moves.
Upvotes: 1
Reputation:
Place your buttons in array, generete random number, so that number would be an button-array index.
Upvotes: 1