2016-03-15 223 views
1

當輪到時間結束時,我遇到了暫停問題。當用戶點擊按鈕時,它開始倒計時器,當完成的文本返回到我想要的值。但是我希望當倒計時(定時器)完成時啓動另一個timer2。現在它開始同時計數。每個計時器在屏幕上都有自己的textView。 這裏是代碼:在倒計時計時器中啓動另一個計時器

button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      long roundtime= Long.parseLong(String.valueOf(msg1))*1000; //User set Time for Round 
      long pause= Long.parseLong(String.valueOf(msg2))*1000; //User set Time for Pause 
      Counting timer = new Counting(roundtime, 1000); // class CountdownTimer 
      Counting2 timer2 = new Counting2(pause, 1000); // class CountdownTimer 

      timer.start(); // I want to start with this timer first 
      timer2.start(); // And when timer is finished start with this in own textView 
     } 

enter image description here

回答

1

使用CountDownTimer的內部類。這是你需要的。只需啓動一個定時器,並在該定時器的onFinish()方法上啓動新的定時器。類似:

CountDownTimer timer1 = new CountDownTimer(30000, 1000) { 

public void onTick(long millisUntilFinished) { 
//do whatever you need here, this gets called every 1000 milliseconds (the 2nd parameter 
of the constructor, the first is total time in ms 
} 

public void onFinish() { 
    CountDownTimer timer2 = new CountDownTimer(5000, 1000) { 
     //same logic 
     }; 
     timer2.start(); 
    } 
}; 

timer1.start();

+0

Hei Vucko!我現在你的意思,但我有兩個不同的倒計時計時器,因爲我有兩個獨立的屏幕上的每個計時器textview。你懂我的意思嗎? –

+0

不是真的,試着找出這個班,這真的很好,我現在睡覺了,明天寫信給你,如果你不能管理隊友:) – Vucko

0

如果我理解正確你的問題,你希望是這樣的。一旦你的活動開始,在'xx'秒後必須發生。然後必須暫停'yy'秒,然後同樣的事情必須'xx'秒。對?

您可以使用Handler.postDelayed()

static boolean pause = false; 

onCreate(){ 
    if (pause) { 
     delay = xx + yy; 
else 
     delay = xx; 


Handler.postdelay(new ... , delayTime); 

,然後很明顯,使pause = true在PostDelay

+0

Rusheel:當用戶點擊該按鈕時,開始倒計時(10秒),並且當完成時,另一個開始倒計時(4秒)。你能看到我更新的圖像嗎? –