簡體   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