Java 如何在android中随机选择按钮?
我想知道如何在android中随机选择一个按钮。例如,有4个按钮,我希望应用程序从中随机选择一个按钮,并对其执行一些操作。这是我的密码:Java 如何在android中随机选择按钮?,java,android,random,Java,Android,Random,我想知道如何在android中随机选择一个按钮。例如,有4个按钮,我希望应用程序从中随机选择一个按钮,并对其执行一些操作。这是我的密码: Button start; ImageButton btn1, btn2, btn3, btn4, btn5; Random random = new Random(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanc
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);
}
}
如果我改成这个,它会工作得很好,但是它只会是btn1,而不是随机选择
@Override
public void onClick(View v) {
if (v == start)
{
btn5 = btn1;
btn5.setBackgroundColor(Color.RED);
}
}
检查上述参考以生成随机数,然后使用
switch(number)
{
case 1:
button.performClick()
.....
}
检查上述参考以生成随机数,然后使用
switch(number)
{
case 1:
button.performClick()
.....
}
您需要将此行放在
onCreate()方法中:
all= {btn1, btn2, btn3, btn4};
现在的情况是:
ImageButton[] all= {btn1, btn2, btn3, btn4};
它在所有按钮均为空的承包商时间内运行。与在onCreate中为新变量分配按钮相比,这不会改变ImageButton。或者你可以写:
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);
您需要将此行放在onCreate()方法中:
all= {btn1, btn2, btn3, btn4};
现在的情况是:
ImageButton[] all= {btn1, btn2, btn3, btn4};
它在所有按钮均为空的承包商时间内运行。与在onCreate中为新变量分配按钮相比,这不会改变ImageButton。或者你可以写:
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
必须在设置了btn1
等之后设置:否则它将是一个null
s数组
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
all
必须在设置了btn1
等之后设置:否则它将是一个null
s数组
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
这样做
ImageButton[] all= {btn1, btn2, btn3, btn4};
Random rand = new Random();
ImageButton randomBtn = all[rand.nextInt(all.length)];
这样做
ImageButton[] all= {btn1, btn2, btn3, btn4};
Random rand = new Random();
ImageButton randomBtn = all[rand.nextInt(all.length)];
我当前运行了以下代码:
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);
}
}
我当前运行了以下代码:
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);
}
}
问题是什么?您是否有任何错误?您需要btn5在单击开始按钮时执行一些操作当我单击该按钮时,应用程序崩溃。您可以检查likev.getId()==R.id.button1
如果您没有收到错误,看起来btn5没有分配给任何按钮有问题吗?您是否有任何错误?您需要btn5在单击开始按钮时执行一些操作当我单击该按钮时,应用程序崩溃。您可以检查像v.getId()==R.id.button1
如果您没有收到错误,看起来btn5没有分配给任何按钮,但如果我将全部={btn1,btn2,btn3,btn4}代码>它出错了。我不能把
ImageButton[]all;`only@user3153613抄过去韦斯顿回答。我们说的是同一件事,我只是解释为什么。但是如果我把all={btn1,btn2,btn3,btn4}代码>它出错了。我不能把
ImageButton[]all;`only@user3153613抄过去韦斯顿回答。我们说的是同一件事,我只是解释一下原因。