Hello社区我开始使用android和android studio。 我是这一切的新手,但我正在努力。
所以这是问题, 我有6个相同尺寸的按钮(70x70dp) 我希望这6个按钮每次都在屏幕的不同位置生成,但不能同时生成。
我已经做过了,但大多数时候,有些按钮比其他按钮高。 我想以某种方式限制这一点。
我已经尝试了一段时间和一些糟糕的检查和所有组合,但到目前为止没有任何工作。
private void changeButtonPlace(Button button) {
Random r = new Random();
int newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
int newYloc = r.nextInt(getScreenHeight() - (3 * button.getLayoutParams().height));
if (button == _button) {
while (Math.abs(newXloc - _button1.getX()) < 10 || Math.abs(newXloc - _button2.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
Log.d("myTag", "KSANA UPOLOGISA");
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button1) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button2.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button2) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button3) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button2.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button2.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button4) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button2.getX()) < 10 || Math.abs(newXloc - _button3.getX()) < 10
|| Math.abs(newXloc - _button5.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button2.getY()) < 10 || Math.abs(newYloc - _button3.getY()) < 10
|| Math.abs(newYloc - _button5.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
} else if (button == _button5) {
while (Math.abs(newXloc - _button.getX()) < 10 || Math.abs(newXloc - _button1.getX()) < 10
|| Math.abs(newXloc - _button2.getX()) < 10 || Math.abs(newXloc - _button4.getX()) < 10
|| Math.abs(newXloc - _button3.getX()) < 10){
newXloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().width));
}
while (Math.abs(newYloc - _button1.getY()) < 10 || Math.abs(newYloc - _button2.getY()) < 10
|| Math.abs(newYloc - _button2.getY()) < 10 || Math.abs(newYloc - _button4.getY()) < 10
|| Math.abs(newYloc - _button3.getY()) < 10){
newYloc = r.nextInt(getScreenWidth() - (2 * button.getLayoutParams().height));
}
}
button.setX(newXloc);
button.setY(newYloc);
}
有什么想法吗? 也许我的想法不对,但主要是我想要的,每次6个按钮都在不同的地方。
答案 0 :(得分:0)
就我而言,你的问题与意图有关。为此,您必须在六个新活动中创建六个按钮中的每一个,然后粘贴此代码
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, sushi.class);
startActivity(i);
}
代替sushi.class,您必须提供新活动的.class名称。