如何随机设置按钮的位置?

时间:2015-03-06 23:12:01

标签: java android button random time

当我点击按钮'开始时间'时,它有时会完全移出屏幕,这是不好的。而且,对我来说,按钮没有随机放置,有时一次点击,它只是向下移动一点。在最后,而不是花时间,应用程序停止与nullpointer。按钮应保留在屏幕上,在点击时随机放置在新位置,点击一下后,我想收到经过的时间。为什么这不起作用?

代码:

@Override

public void onClick(View v) {

Button b = (Button) findViewById(R.id.start_time);

    Random r = new Random();
    View decorView = getWindow().getDecorView();
    int screenWidth = decorView.getWidth();
    int screenHeight = decorView.getHeight();
      long startTime =  SystemClock.elapsedRealtime();
      i++;
/*  
        Random r = new Random();

    int x = r.nextInt(R.id.wrap_content);
      int y = r.nextInt(R.id.wrap_content);




    b.setX(x);  
    b.setY(y);
     */ 

    if (i == 1 ) {

        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 2 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 3 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 4 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 5 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 6 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    else if (i == 7) {
        long difference = SystemClock.elapsedRealtime()  - startTime;

          Intent intent = new Intent(Game.this, MainScreen.class);
          intent.putExtra("time",difference);
         // Toast.makeText(getApplicationContext(), getIntent().getStringExtra("time"), Toast.LENGTH_LONG).show();
          textview1.setText(getIntent().getStringExtra("time"));
        finish();
    }


}

和xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="chdfss.dsfwegg.trhhGame" >

    <Button
        android:id="@+id/start_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="139dp"
        android:text="Button" />




</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

我不会使用保证金。这是旧的方式。如果您支持Android 3.0+,则可以使用View属性将它们放在任何位置。

这样的事情:

Random rand = new Random();
View decorView = getWindow().getDecorVew();
int screenWidth = decorView.getWidth();
int screenHeight = decorView.getHeight();
...
// when you want to move the button.

view.setX(rand.nextInt(screenWidth - view.getWidth());
view.setY(rand.nextInt(screenHeight - view.getHeight());

View#setX()View#setY()方法链接到视图的左上角。

如果你想支持少于3.0,那么你可以使用NineOldAndroids动画api和动画,时间为0毫秒,这样就可以了。