2016-04-27 117 views
0

Hello社区我开始使用android和android studio。 我对这一切都很陌生,但我在努力。不同位置的按钮

因此,这里的问题,我 具有相同的尺寸(70x70dp) 的6个按钮,我想在屏幕的不同位置,但不在同一个生成的每个时间这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

这看起来太复杂了。无论如何,为什么不只是存储现有按钮的坐标,并且只要坐标相交就重新放置? – m02ph3u5

+0

感谢您的快速回复,也许我可以在每个相交的X,Y位置存储一个数组,然后如您所说每次检查它是否相同,但如果我没有错,按钮位置具体为1个坐标,例如可以是500,500,如果代码生成501,501也是一个问题。 – usrnvm43894

+0

所以也许,我需要检查每次加70 dp每个维度 – usrnvm43894

回答

0

就我而言,您的问题与Intent有关。对于必须在六个新的活动创建这六个按钮和比粘贴此代码

public void onClick(View view) { 
         Intent i = new Intent(MainActivity.this, sushi.class); 
         startActivity(i); 
        } 

代替sushi.class你必须提供新的活动的.class名字。

+0

也许我没有exaplain好,我的问题 – usrnvm43894

+0

继承人我想做什么http://prntscr.com/axjlmy 这里是有多少时间来http ://prntscr.com/axjm14 – usrnvm43894

+0

你也可以通过Intent来做到这一点。创建第一个截屏作为一个活动,第二个截屏作为另一个活动,onClick可以显示第二个活动。 –