Java 在布局中随机放置一个按钮
我的代码是这样工作的:buttongame1是开始时间在下一个i上移动的地方。但是buttongame1应该随机选择它的位置,它会这样做,但不是在i==0时,所以每次我打开活动时,buttongame1都不是随机放置的Java 在布局中随机放置一个按钮,java,android,button,if-statement,random,Java,Android,Button,If Statement,Random,我的代码是这样工作的:buttongame1是开始时间在下一个i上移动的地方。但是buttongame1应该随机选择它的位置,它会这样做,但不是在i==0时,所以每次我打开活动时,buttongame1都不是随机放置的 public void onClick(View v) { textview1 = (TextView) findViewById(R.id.over); Random r = new Random(); RelativeL
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();
如果没有i==0的任何代码,请在i==0中设置随机位置。在调用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();
}
}
然后是我的其余代码