Java 在Android Studio中设置倒计时
我正在尝试创建一个在左上角有倒计时计时器的游戏 我已经创建了一个文本视图框,并将其命名为“时间”,就我所知,我已经正确设置了计时器,但它没有显示任何内容 我的代码如下:Java 在Android Studio中设置倒计时,java,android,timer,Java,Android,Timer,我正在尝试创建一个在左上角有倒计时计时器的游戏 我已经创建了一个文本视图框,并将其命名为“时间”,就我所知,我已经正确设置了计时器,但它没有显示任何内容 我的代码如下: public class GameView extends SurfaceView implements SurfaceHolder.Callback { /* Member (state) fields */ private GameLoopThread gameLoopThread; private Paint pai
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
/* Member (state) fields */
private GameLoopThread gameLoopThread;
private Paint paint; //Reference a paint object
/** The drawable to use as the background of the animation canvas */
private Bitmap mBackgroundImage;
// For creating the game Sprite
private Sprite sprite;
// For recording the number of hits
private int hitCount;
public GameView(Context context) {
super(context);
// Focus must be on GameView so that events can be handled.
this.setFocusable(true);
// For intercepting events on the surface.
this.getHolder().addCallback(this);
// Background image added
mBackgroundImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.half_moon);
}
/* Called immediately after the surface created */
public void surfaceCreated(SurfaceHolder holder) {
// We can now safely setup the game start the game loop.
ResetGame();//Set up a new game up - could be called by a 'play again option'
mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, getWidth(), getHeight(), true);
gameLoopThread = new GameLoopThread(this.getHolder(), this);
gameLoopThread.running = true;
gameLoopThread.start();
}
// For the countdown timer
private long startTime; // Timer to count down from
private final long interval = 1 * 1000; // 1 sec interval
private CountDownTimer countDownTimer; // Reference to the class
private boolean timerRunning = false;
private String displayTime; // To display the time on the screen
private TextView time = (TextView) findViewById(R.id.time);
// Countdown Timer - private class
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer (long startTime, long interval) {
super(startTime, interval);
}
public void onFinish() {
displayTime = "Time is up!";
timerRunning = false;
countDownTimer.cancel();
}
public void onTick (long millisUntilFinished) {
displayTime = " " + millisUntilFinished / 1000;
}
}
//To initialise/reset game
private void ResetGame(){
/* Set paint details */
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
sprite = new Sprite(this);
hitCount = 0;
// Set timer
startTime = 10; // Start at 10s to count down
// Create new object - convert startTime to milliseconds
countDownTimer = new MyCountDownTimer(startTime*1000, interval);
countDownTimer.start(); // Start the time running
timerRunning = true;
}
//This class updates and manages the assets prior to drawing - called from the Thread
public void update(){
sprite.update();
}
/**
* To draw the game to the screen
* This is called from Thread, so synchronisation can be done
*/
public void doDraw(Canvas canvas) {
//Draw all the objects on the canvas
canvas.drawBitmap(mBackgroundImage, 0, 0, null);
canvas.drawText("The Game ",15,25, paint);
sprite.draw(canvas);
}
//To be used if we need to find where screen was touched
public boolean onTouchEvent(MotionEvent event) {
if (sprite.wasItTouched(event.getX(), event.getY())) {
// This just renews the sprite for now
sprite = new Sprite(this);
hitCount++;
}
return true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
gameLoopThread.running = false;
// Shut down the game loop thread cleanly.
boolean retry = true;
while(retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {}
}
}
public void getHitCount() {
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
}
一如既往,非常感谢您的帮助。在
onTick
方法中,将displayTime
的值分配到TextView
用法:
time.setText(显示时间)
在onTick
方法中,将displayTime
的值分配到TextView
用法:
time.setText(显示时间)代码>//这是用于16秒倒计时的
final int sec = 16000;
countDownTimer = new CountDownTimer(sec, 1000) {
public void onTick(long millisUntilFinished) {
int minute = (int) (millisUntilFinished / 1000) / 60;
int second = (int) (millisUntilFinished / 1000) % 60;
mTimer.setText(String.format("%02d", minute) + ":" + String.format("%02d", second));
}
public void onFinish() {
commonActions.showToast("Successfully Finished");
}
};
并通过调用countDownTimer.start()启动计时器代码>
如果这不能满足您的需要。请告诉我//这是一个16秒的倒计时
final int sec = 16000;
countDownTimer = new CountDownTimer(sec, 1000) {
public void onTick(long millisUntilFinished) {
int minute = (int) (millisUntilFinished / 1000) / 60;
int second = (int) (millisUntilFinished / 1000) % 60;
mTimer.setText(String.format("%02d", minute) + ":" + String.format("%02d", second));
}
public void onFinish() {
commonActions.showToast("Successfully Finished");
}
};
并通过调用countDownTimer.start()启动计时器代码>
如果这不符合您的需要。请告诉我您尚未在textView中设置文本。因此,如果您没有在textView中设置文本,它将显示空textView。所以它会显示空文本视图谢谢。但是,添加此行会使应用程序崩溃,并且不会显示时间。我认为这是空指针异常@PhilAdams你能发布logcat日志吗?@PhilAdams更新了答案,请现在查看。Yogesh现在它可能工作了我使用了你更新的答案,它不再崩溃,但仍然不显示timer@PhilAdams请检查我的最新答案。已在onTick上添加日志。所以,你们需要在运行应用程序时检查日志,以确保logcat是否正在打印该日志。若并没有遇到日志,那个么问题在于时间开始感谢。但是,添加此行会使应用程序崩溃,并且不会显示时间。我认为这是空指针异常@PhilAdams你能发布logcat日志吗?@PhilAdams更新了答案,请现在查看。Yogesh现在它可能工作了我使用了你更新的答案,它不再崩溃,但仍然不显示timer@PhilAdams请检查我的最新答案。已在onTick上添加日志。所以,你们需要在运行应用程序时检查日志,以确保logcat是否正在打印该日志。若并没有遇到日志,那个么问题在于开始时间
final int sec = 16000;
countDownTimer = new CountDownTimer(sec, 1000) {
public void onTick(long millisUntilFinished) {
int minute = (int) (millisUntilFinished / 1000) / 60;
int second = (int) (millisUntilFinished / 1000) % 60;
mTimer.setText(String.format("%02d", minute) + ":" + String.format("%02d", second));
}
public void onFinish() {
commonActions.showToast("Successfully Finished");
}
};