繁体   English   中英

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

[英]how to use timer in minutes and seconds

我在Android应用程序中使用了计时器。

这就是我正在使用的

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秒。

所以,怎么做。 请帮我。

提前致谢。

一种获取所需String的方法看起来像.-

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM