2016-04-27 11 views
0

こんにちはコミュニティ私はアンドロイドとアンドロイドのスタジオで作業を始めました。 私はこのすべてに新しいですが、私は試しています。異なる場所にあるボタン

ここに問題があります 私は同じサイズ(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のlocsを交差して格納することができますし、あなたは毎回同じようにチェックしたが、私が間違っていない場合、ボタンの位置は特に1コーディネートたとえば、コードが501,501を生成する場合は500,500になりますが、これも問題になります。 – usrnvm43894

+0

多分、私は両方の次元のためにプラス70dp毎回チェックする必要があります – usrnvm43894

答えて

0

私の知る限り、あなたの質問はインテントに関連しています。そのためにあなたがsushi.classの代わりに6つの新しい活動にして貼り付けるよりも、このコード

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

を6つのボタンのそれぞれを作成する必要がありますが、新たな活動の.classファイル名を指定する必要があります。

+0

多分、私はよくexaplainをしなかった – usrnvm43894

+0

heres私はhttp://prntscr.com/axjlmyを作りたいと思った そしてここにいくつかの時http ://prntscr.com/axjm14 – usrnvm43894

+0

これはインテントでも行うことができます。最初のスクリーンショットを1つのアクティビティとして作成し、2つ目のスクリーンショットを別のアクティビティとして作成します。onClick 2つ目のアクティビティを表示できます。 –

関連する問題