android - 如何在应用程序中添加计时器

标签 android timer

我是一名新的安卓开发者。最近上线了一款字谜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/

相关文章:

java - Android 创建目录层次结构

Android:无法在外部存储上创建文件

android - 如何强制 Jacoco 在 Gradle 中使用特定版本

android - 从 Intent.ACTION_GET_CONTENT 读取 Uri 是否需要 READ_EXTERNAL_STORAGE 权限

java - Thread.sleep() 到 Swing 定时器的转换

javascript - qtScript 上 JavaScript 的 setTimeout 的等价物是什么?

ios - 后台的 Swift 定时器

c# - 如果使用 System.Threading.Timer,是否需要 BackgroundWorker?

android - 在 Android 应用程序中集成 Facebook 和 LinkedIn

javascript - 应用程序在后台时 setInterval 函数不运行