![](/img/trans.png)
[英]How can I make my Java program pause until a specific button has been pressed?
[英]How do I make my buttons show up in random places until the button is pressed?
我基本上什么都試過了! 我正在嘗試制作一個簡短的迷你游戲,您必須按下屏幕上彈出的圖像。 圖像隨機移動到應用程序的不同點,但問題是我無法繼續移動圖像! 順便說一下,圖片是一個按鈕!
游戲視圖
package interactive.siddiqui.fortuneteller;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.util.Random;
public class GameView extends Activity{
private Button btn;
private boolean tf = false;
private final int SPLASH_DISPLAY_LENGTH = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_view);
boolean foo = true;
while(foo = true) Click();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game_view, menu);
return true;
}
public void Click() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Button btn = (Button) findViewById(R.id.button9);
Random r = new Random();
int x = r.nextInt(480);
int y = r.nextInt(800);
btn.setX(x);
btn.setY(y);
}
}, SPLASH_DISPLAY_LENGTH);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void bob(View view){
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(GameView.this, Finished.class);
startActivity(intent);
tf = true;
}
});
}
}
這主要適用於Java,但我也會在相應的XML文件中添加。
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="interactive.siddiqui.fortuneteller.GameView"
android:background="@drawable/background_fortune">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/button9"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@drawable/small_fortune"
android:onClick="bob"
android:layout_x = "0dp"
android:layout_y = "0dp"/>
</RelativeLayout>
我該怎么做呢? 我只希望按鈕出現在隨機位置,直到單擊按鈕! 順便說一下,這不是家庭作業......
使用 Timer 和 TimerTask,而不是使用鎖定 UI 線程的無限 while(因此您不會看到該位置的任何更新)。
此外,您的bob
方法,您正在分配一個新的點擊偵聽器,我認為這不是您真正想要的。
無論如何,試試這個,如果你對代碼有任何疑問,請問:)
package interactive.siddiqui.fortuneteller;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class GameView extends Activity{
private Button btn;
private boolean tf = false;
private boolean canMove = true;
private Timer timer;
private final long SPLASH_DISPLAY_LENGTH = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_view);
start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game_view, menu);
return true;
}
public void start()
{
canMove = true;
timer = new Timer();
timer.scheduleAtFixedRate(
new TimerTask()
{
@Override
public void run()
{
moveButton();
}
},
SPLASH_DISPLAY_LENGTH,
SPLASH_DISPLAY_LENGTH
);
}
private void moveButton()
{
if(!canMove){ return; }
runOnUiThread(
new Runnable()
{
@Override
public void run()
{
Button btn = (Button)findViewById(R.id.button9);
Random r = new Random();
int x = r.nextInt(480);
int y = r.nextInt(800);
btn.setX(x);
btn.setY(y);
}
}
);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void bob(View view)
{
canMove = false;
if(timer != null)
{
try
{
timer.cancel();
timer.purge();
}catch(Exception e){ e.printStackTrace(); }
timer = null;
}
Intent intent = new Intent(GameView.this, Finished.class);
startActivity(intent);
tf = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.