如何设置一个按钮的位置随机新?
how to set a button's position randomly new?
当我点击按钮 'start time' 时,它有时会完全移出屏幕,这很糟糕。而且,对我来说,按钮的放置不够随机,有时单击一下,它只是向下移动了一点。最后,应用程序没有得到时间,而是以 nullpointer 停止。 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>
我不会使用边距。那是老办法。如果您支持 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 毫秒的时间进行动画处理,因此它是即时的。
当我点击按钮 'start time' 时,它有时会完全移出屏幕,这很糟糕。而且,对我来说,按钮的放置不够随机,有时单击一下,它只是向下移动了一点。最后,应用程序没有得到时间,而是以 nullpointer 停止。 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>
我不会使用边距。那是老办法。如果您支持 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 毫秒的时间进行动画处理,因此它是即时的。