简体   繁体   English

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

[英]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.

相关问题 如何在倒数计时器中传递秒值,我可以传递分钟但不传递秒,它总是从60开始 - How to pass values for seconds in countdown timer , I am able to pass minutes but not seconds , it always start from 60 android eclipse:倒数计时器,以小时,分钟和秒为单位 - android eclipse: Countdown timer in hours, minutes and seconds 计时器减少文本视图中显示的时间,以分钟和秒为单位 - Timer decrease time displayed in textview with minutes and seconds 如何重置计时器中的秒数 - How to reset seconds in timer 如何修改TimePicker以使用分钟和秒而不是小时和分钟 - How can I modify TimePicker to use minutes and seconds instead hour and minutes 如何在片段内使用 2 秒的计时器? - How can I use a Timer of 2 seconds inside a fragment? 如何在倒数计时器中显示分钟和秒 - How to display minutes and seconds in countdowntimer textview中的显示计时器包含android中的天,小时,分钟和秒 - Display timer in textview contain days, hours, minutes and seconds in android 以HH显示倒数计时器:MM:SS(小时:分钟:秒) - Show Countdown timer in HH:MM:SS (Hours : Minutes : Seconds) 如何在android中使用计时器,这会导致程序在按下按钮时运行2分钟,然后在两分钟后自动停止 - how to use a timer in android which causes a program to run for 2 minutes when a button is pressed and then stop automatically after two minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM