开发者

Randomly perform a click on a button in android

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 开发者_JS百科figure out how to randomly select some number of buttons and do its performClick() method. Does anyone have any ideas?

Thank you Billy


Place your buttons in array, generete random number, so that number would be an button-array index.


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:

  1. Move the "change color" logic inside each button click handler into a method alterState(int actionId);

  2. Call alterState() within each button's click handler

  3. When the app starts, call alterState(new Random().nextInt() % NUM_ACTIONS) in a loop to perform your random moves.


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();

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜