【问题标题】:place a button randomly in layout在布局中随机放置一个按钮
【发布时间】:2015-03-28 06:55:32
【问题描述】:

我的代码是这样工作的:buttongame1 是 start_time 在下一个 i 上移动的地方。但是 buttongame1 应该随机选择它的位置,它会这样做,但不是在 i == 0 上,所以每次我打开活动时,buttongame1 都不是随机放置的

public void onClick(View v) {

         textview1 = (TextView) findViewById(R.id.over);

        Random r = new Random();

        RelativeLayout decorView = (RelativeLayout) buttongame1.getParent();

        int screenWidth = decorView.getWidth();
        int screenHeight = decorView.getHeight();



          if(v == gameover){


              i--;

                btncounter.setText("GAME OVER");
                start_time.setVisibility(View.GONE);
                buttongame1.setVisibility(View.GONE);
                textview1.setVisibility(View.VISIBLE);
                Animation anim = new AlphaAnimation(0.0f, 1.0f);
                anim.setDuration(50); //You can manage the time of the blink with this parameter
                anim.setStartOffset(20);
                anim.setRepeatMode(Animation.REVERSE);
                anim.setRepeatCount(123);
                textview1.startAnimation(anim);
                Toast.makeText(this, "GAME OVER", Toast.LENGTH_SHORT).show();
                over1.start();



                        finish();


                    }

          if (i == 0 ) {
                btncounter.setText("0");

                //int x0 = (int) buttongame1.getX();
                //int y0 = (int) buttongame1.getY();


            }
          i++;

        if (i == 1 ) {
            btncounter.setText("1");
             startTime = System.currentTimeMillis();
             int x1 = (int) buttongame1.getX();
                int y1 = (int) buttongame1.getY();
                start_time.setX(x1);
                start_time.setY(y1);
                 buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
                    buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));

                    mp1.start();
        }
        if (i == 2 ) {
            btncounter.setText("2");
            int x2 = (int) buttongame1.getX();
            int y2 = (int) buttongame1.getY();
            start_time.setX(x2);
            start_time.setY(y2);
             buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
            buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
            mp2.start();
        }
        if (i == 3 ) {
            btncounter.setText("3");
            int x3 = (int) buttongame1.getX();
            int y3 = (int) buttongame1.getY();
            start_time.setX(x3);
            start_time.setY(y3);
             buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
            buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
            mp3.start();
        }
        if (i == 4 ) {
            btncounter.setText("4");
            int x4 = (int) buttongame1.getX();
            int y4 = (int) buttongame1.getY();
            start_time.setX(x4);
            start_time.setY(y4);
             buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
            buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
            mp4.start();
        }
        if (i == 5 ) {
            btncounter.setText("5");
            int x5 = (int) buttongame1.getX();
            int y5 = (int) buttongame1.getY();
            start_time.setX((x5));
            start_time.setY(y5);
             buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
            buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
            mp5.start();
        }
        if (i == 6 ) {
            btncounter.setText("6");
            int x6 = (int) buttongame1.getX();
            int y6 = (int) buttongame1.getY();
            start_time.setX(x6);
            start_time.setY(y6);
             buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
            buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
            mp6.start();
        }
        if (i == 7 ) {
            btncounter.setText("7");
            int x7 = (int) buttongame1.getX();
            int y7 = (int) buttongame1.getY();
            start_time.setX(x7);
            start_time.setY(y7);
             buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
            buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
            mp7.start();
        }
        if (i == 8 ) {
            btncounter.setText("8");
            int x8 = (int) buttongame1.getX();
            int y8 = (int) buttongame1.getY();
            start_time.setX(x8);
            start_time.setY(y8);
             buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
            buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
            mp8.start();
        }
        if (i == 9 ) {
            btncounter.setText("9");
            int x9 = (int) buttongame1.getX();
            int y9 = (int) buttongame1.getY();
            start_time.setX(x9);
            start_time.setY(y9);
            mp9.start();


        }

        else if (i == 10) {
            mp10.start();
            btncounter.setText("10");
            start_time.setVisibility(View.GONE);
            buttongame1.setVisibility(View.GONE);

            long difference = (System.currentTimeMillis()  - startTime);



            String thetime = String.format("%d,%03d sec",difference/1000,difference%1000);

            Toast.makeText(this, (thetime), Toast.LENGTH_SHORT).show();

【问题讨论】:

    标签: java android button if-statement random


    【解决方案1】:

    你没有任何 i==0 的代码,在 i==0 中设置随机位置..

    【讨论】:

      【解决方案2】:

      我必须在调用 onClick 之前放置 (i =0)。当我这样做时,一切正常。

      public class Game extends Activity implements OnClickListener{
      
          Button start_time;
          Button buttongame1;
          int i;
      
      
             Random r;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
      
      
      
      
              super.onCreate(savedInstanceState);
      
              setContentView(R.layout.activity_game);
                start_time = (Button) findViewById(R.id.start_time);
                start_time.setOnClickListener(this);
                textview1 = (TextView) findViewById(R.id.over);
                gameover = (Button) findViewById(R.id.gameover);
                gameover.setOnClickListener(this);
      
                i = 0;
                r = new Random();
             buttongame1 = (Button) findViewById(R.id.buttongame1);
             btn_start2 = (Button) findViewById(R.id.btn_start2);
      
      
               smthin();  
      
      
          }
      
      
          public void smthin() {
      
      
               DisplayMetrics displaymetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                int height = displaymetrics.heightPixels;
                int Width = displaymetrics.widthPixels;
      
      
                          try { 
      
                              if (i == 0) {
                            buttongame1.setX(r.nextInt(Width - buttongame1.getHeight()));
                              buttongame1.setY(r.nextInt(height - buttongame1.getWidth()));
      
                              }
      
      
                          } catch (Exception e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
      
          } 
      

      接下来是我的其余代码。

      【讨论】:

        猜你喜欢
        • 2016-06-24
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 2016-09-02
        • 2021-04-14
        • 2012-04-20
        • 1970-01-01
        相关资源
        最近更新 更多