gpt4 book ai didi

java - 如何设置一个按钮的位置随机新?

转载 作者:行者123 更新时间:2023-11-30 08:55:40 24 4
gpt4 key购买 nike

当我点击“开始时间”按钮时,它有时会完全移出屏幕,这很糟糕。而且,对我来说,按钮的放置不够随机,有时单击一下,它只是向下移动了一点。最后,应用程序没有得到时间,而是以 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() 方法链接到 View 的左上角。

如果你想支持低于 3.0 的版本,那么你可以使用 NineOldAndroids动画 api 和动画,时间为 0 毫秒,因此它是即时的。

关于java - 如何设置一个按钮的位置随机新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28909226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com