如何向app添加计时器

时间:2012-08-22 23:36:59

标签: android timer

我是一名新的Android开发人员。最近,推出了一个单词拼图应用程序 - Wozzle

我想为游戏添加计时器,以便玩家获得有限的时间进行移动。怎么做 ?此外,计时器应显示给播放器。

1 个答案:

答案 0 :(得分:4)

试试这个,

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);

        }

    }
}

看一下这些链接