我是一名新的安卓开发者。最近上线了一款字谜app-Wozzle
我想在游戏中添加计时器,以便玩家在有限的时间内采取行动。怎么做 ?此外,计时器应显示给玩家。
最佳答案
试试这个,
public class CountDownTest extends Activity {
TextView tv; // textview to display the countdown
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000, 1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished/1000);
}
}
}
看看这些链接
关于android - 如何在应用程序中添加计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12082831/