[英]how to use timer in minutes and seconds
I am using a Timer in my android app. 我在Android应用程序中使用了计时器。
This is what i am using, 这就是我正在使用的
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);
In my code, there's only seconds
as you can see But I want it in Minutes and seconds like 00:01
. 在我的代码中,您可以看到只有
seconds
,但是我想要分钟和00:01
秒。
So, how to do it. 所以,怎么做。 Please help me.
请帮我。
Thanks in Advance. 提前致谢。
An approach to obtain the String
you're looking for, would look like.- 一种获取所需
String
的方法看起来像.-
int seconds = time % 60;
int minutes = time / 60;
String stringTime = String.format("%02d:%02d", minutes, seconds);
tv.setText(stringTime);
If you need to show the results only in your second Activity
, I'd recommend passing time value into args bundle, and generate the String
from the activity which will display it. 如果只需要在第二个
Activity
显示结果,建议将时间值传递到args包中,并从活动中生成String
来显示它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.