1

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

だから、それを行う方法。私を助けてください。

前もって感謝します。

4

1 に答える 1

1

String探しているものを取得するためのアプローチは、次のようになります。

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

2 番目の でのみ結果を表示する必要がある場合は、Activity時間値を args バンドルに渡し、Stringそれを表示するアクティビティから を生成することをお勧めします。

于 2013-09-28T13:34:33.203 に答える