简体   繁体   English

如何在Android中随机选择按钮?

[英]How to randomly select buttons in android?

I wanted to know how to randomly select a button in android. 我想知道如何在android中随机选择一个按钮。 Like for example there got 4 button, I want the application to randomly choose a button from them and do some actions to it. 例如,有4个按钮,我希望应用程序从中随机选择一个按钮并对其执行一些操作。 Here is my code: 这是我的代码:

Button start;
ImageButton btn1, btn2, btn3, btn4, btn5;
Random random = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memory);
    start = (Button)findViewById(R.id.button1);
    start.setOnClickListener(this);
    btn1 = (ImageButton)findViewById(R.id.imageButton1);
    btn2 = (ImageButton)findViewById(R.id.imageButton2);
    btn3 = (ImageButton)findViewById(R.id.imageButton3);
    btn4 = (ImageButton)findViewById(R.id.imageButton4);

}

ImageButton[] all= {btn1, btn2, btn3, btn4};

@Override
public void onClick(View v) {
    if (v == start)
    {
        btn5 = all[random.nextInt(all.length)];
        btn5.setBackgroundColor(Color.RED);
    }
}

If I change to this it work perfectly but then it will only be btn1 and not randomly select. 如果我更改为它,它可以完美工作,但是它将只是btn1而不是随机选择。

@Override
public void onClick(View v) {
    if (v == start)
    {
        btn5 = btn1;
        btn5.setBackgroundColor(Color.RED);
    }
}

You need to place this line inside your onCreate() method: 您需要将此行放在onCreate()方法中:

all= {btn1, btn2, btn3, btn4};

As it is now: 现在是这样:

ImageButton[] all= {btn1, btn2, btn3, btn4};

It runs in the contractor time where all the buttons are null. 它在所有按钮都为空的承包商时间内运行。 Than in the onCreate you assign the buttons for new variables, how ever this does not change ImageButton. 与在onCreate中为新变量分配按钮相比,这不会改变ImageButton。 Alternativly you could wrote: 或者,您可以这样写:

ImageButton[0] = (ImageButton)findViewById(R.id.imageButton1);
ImageButton[1] = (ImageButton)findViewById(R.id.imageButton2);
ImageButton[2] = (ImageButton)findViewById(R.id.imageButton3);
ImageButton[3] = (ImageButton)findViewById(R.id.imageButton4);

all must be set after you have set btn1 etc: Otherwise it'll be an array of null s. all已设置必须设置btn1否则这将是一个数组:等null秒。

    btn1 = (ImageButton)findViewById(R.id.imageButton1);
    btn2 = (ImageButton)findViewById(R.id.imageButton2);
    btn3 = (ImageButton)findViewById(R.id.imageButton3);
    btn4 = (ImageButton)findViewById(R.id.imageButton4);
    all= {btn1, btn2, btn3, btn4}; //here
}

ImageButton[] all; //not here

How do I generate random integers within a specific range in Java? 如何在Java中生成特定范围内的随机整数?

check the above reference for generating random number then use 检查上面的参考以生成随机数,然后使用

switch(number)
{
case 1:
button.performClick()

.....
}

do this way 这样做

 ImageButton[] all= {btn1, btn2, btn3, btn4};

 Random rand = new Random();
 ImageButton randomBtn = all[rand.nextInt(all.length)];

There is following code which i have run currently: 我目前正在运行以下代码:

    Button start;
    ImageButton btn1, btn2, btn3, btn4, btn5;
    Random random;

    int[] all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        random = new Random();

        start = (Button)findViewById(R.id.button1);

        start.setOnClickListener(this);

        btn1 = (ImageButton)findViewById(R.id.ImageButton01);
        btn2 = (ImageButton)findViewById(R.id.ImageButton02);
        btn3 = (ImageButton)findViewById(R.id.ImageButton03);
        btn4 = (ImageButton)findViewById(R.id.ImageButton04);

        all = new int[]{R.id.ImageButton01,R.id.ImageButton02,R.id.ImageButton03,R.id.ImageButton04};
    }



    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1)
        {
            int id = all[random.nextInt(all.length)];
            findViewById(id).setBackgroundColor(Color.BLACK);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM