如何在几分钟和几秒钟内使用计时器

时间:2013-09-28 12:58:35

标签: android timer textview timertask

我在我的Android应用程序中使用了一个Timer。

这就是我正在使用的,

Timer t = new Timer();
 //Set the schedule function and rate
 t.scheduleAtFixedRate(new TimerTask() {

      public void run() 
     {
         //Called each time when 1000 milliseconds (1 second) (the period parameter)
         runOnUiThread(new Runnable() {

                public void run() 
                {
                    TextView tv = (TextView) findViewById(R.id.timer);
                    tv.setText(String.valueOf(time));
                    time += 1;

                }

            });
     }

 },
 //Set how long before to start calling the TimerTask (in milliseconds)
 0,
 //Set the amount of time between each execution (in milliseconds)
 1000); 

在我的代码中,您只能看到seconds但我希望它在分钟和秒钟中显示00:01

那么,怎么做呢。请帮帮我。

先谢谢。

1 个答案:

答案 0 :(得分:1)

获得您正在寻找的String的方法看起来像.-

int seconds = time % 60;
int minutes = time / 60;
String stringTime = String.format("%02d:%02d", minutes, seconds);
tv.setText(stringTime);

如果您只需要在第二个Activity中显示结果,我建议将时间值传递给args包,并从显示它的活动中生成String

相关问题