【发布时间】:2014-06-17 12:07:49
【问题描述】:
我创建了一个有 6 个按钮的游戏,当您单击按钮 2 时按钮应该改变它们的位置,这是我所做的脚本:
if (id == R.id.button2) {
action1();
action2();
action3();
action4();
action5();
action6();
}
下面是动作:
private void action1() {
// TODO Auto-generated method stub
Button button1 = (Button)findViewById(R.id.button1);
button1.setX(50);
int min = 0;
int max = 200;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
button1.setY(i1);
}
private void action2() {
// TODO Auto-generated method stub
Button button2 = (Button)findViewById(R.id.button2);
button2.setX(50);
int min = 0;
int max = 1200;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
button2.setY(i1); }
private void action3() {
// TODO Auto-generated method stub
Button button3 = (Button)findViewById(R.id.button3);
button3.setX(50);
int min = 400;
int max = 600;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
button3.setY(i1); }
private void action4() {
// TODO Auto-generated method stub
Button button4 = (Button)findViewById(R.id.button4);
button4.setX(50);
int min = 600;
int max = 800;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
button4.setY(i1); }
private void action5() {
// TODO Auto-generated method stub
Button button5 = (Button)findViewById(R.id.button5);
button5.setX(50);
int min = 800;
int max = 1000;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
button5.setY(i1); }
private void action6() {
// TODO Auto-generated method stub
Button button6 = (Button)findViewById(R.id.button6);
button6.setX(50);
int min = 1000;
int max = 1200;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
button6.setY(i1);
}
我的问题是:按钮改变了它们的位置,因此您无法按下按钮,我该怎么做才能解决这个问题?
我想过这个选项:
得到按钮和顶部的Y坐标,除以6,然后我知道我应该给每个按钮的Y坐标范围是多少,我该怎么做?或者你能告诉我一个更好的方法吗?
【问题讨论】:
标签: android button random action coordinate