【问题标题】:how to set a button's position randomly new?如何随机设置按钮的位置?
【发布时间】:2015-03-06 23:12:01
【问题描述】:

当我单击“开始时间”按钮时,它有时会完全移出屏幕,这很糟糕。而且,对我来说,按钮的放置不够随机,有时只需单击一下,它就会向下移动一点。最后,应用程序没有得到时间,而是以空指针停止。 Button 应该留在屏幕上,点击时随机放置在新位置,点击几下后,我想收到经过的时间。为什么这不起作用?

代码:

@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>

【问题讨论】:

  • 请解释为什么您使用 3 种不同的方式来生成随机位置
  • 第一次是我自己的自制软件。第二次屏幕边缘不是按钮的边框。在今晚的最新研究之后,我现在将以我认为最简单的方式来做这件事

标签: java android button random time


【解决方案1】:

我不会使用边距。那是老办法。如果您支持 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 毫秒的时间进行动画处理。

【讨论】:

  • 谢谢。现在有 2 个问题:int screenWidth = getScreenWidth(); Game :: 类型的 getScreenWidth() 方法未定义,并且:view.setX(r.nextInt(screenWidth - view.getWidth()); 无法解析视图
  • getScreenWidth() 和 view 是您必须创建的引用。您可以找到在 SO 中获得全宽的方法。 view 是对要移动的视图的引用。
  • 哦..没办法,谢谢。要么我太累了,要么太愚蠢了大声笑。最后一个问题 2day: 1. getScreenWidth(int) 方法仍未定义.. 2. 我可以像int screenWidth = getScreenWidth(900); 一样自行设置长度等,对吗?但是如何自动检测它们呢?我必须用 wrap_content 创建一个视图吗?
  • 您好,先生。你带给我的一段很好的代码。谢谢你。但有时按钮只是移出屏幕......为什么?我以为显示器设置好了? (在问题中更新了代码)
  • 坐标映射到容器视图,所以如果容器视图有任何边距或任何东西,那么您需要使用容器视图的宽度和高度,而不是屏幕的宽度和高度。
猜你喜欢
  • 1970-01-01
  • 2021-09-14
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
相关资源
最近更新 更多