2017-06-12 124 views
4

我有一個CountDown計時器,以10000秒爲單位向下計數,以1秒爲增量遞減,以使按鈕在10秒後可點擊。雖然計時器是準確的,並且做了什麼代碼說,我想改變秒的表達方式,但我不知道如何。讓倒數計時器從10秒變爲1秒

的java:

void startTimer() { 
    cTimer = new CountDownTimer(10000, 1000) { 
     public void onTick(long millisUntilFinished) { 
      c.setText("Please wait " + millisUntilFinished/1000 + " seconds"); 
      thx.setText(millisUntilFinished/1000 + ""); 
      thx.setAlpha(.5f); 
      thx.setClickable(false); 

     } 
     public void onFinish() { 
     c.setText("done"); 
      thx.setText("ready"); 
      thx.setAlpha(1f); 
      thx.setClickable(true); 
     } 
    }; 
    cTimer.start(); 
} 

輸出(每秒):9, 8, 7, 6, 5, 4, 3, 2, 1, (still 1), ready

期望:(每秒):10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ready

謝謝,

編輯:

我加入基於倒計時,thx.setText(((millisUntilFinished/1000) + 1) + "");

新的輸出:10, 9, 8, 7, 6, 5, 4 ,3 , 2, (Still 2), ready

更緊密......但並不完全。

+0

你能看到什麼'thx。setText(millisUntilFinished +「」);'?沒有除法 – Vyacheslav

+0

9901,8901,7900,6897,5896,4890,3890,2889,1888,(1888),就緒 –

+0

如何實例化開始時間例如, 10000計時器上方,然後打勾,您可以將該值減少1000,然後將該數字除以輸出。這會給你更多的控制和精度。 –

回答

0

這只是我對CountDownTimer調查時,我才和我的應用程序的工作蠻好用的CountDownTimer 幾個月。

public void onTick(long millisUntilFinished) 

這millisUntilFinished將給予毫秒的剩餘時間,以及最近的1000毫秒是調用onFinish()方法,所以onTick方法將被調用,直到剩下的時間比(1000(用於OnFinish)+ 1000多(對於計數器))毫秒,如果最後剩餘的毫秒小於2000毫秒,它將跳過onTick(),當定時器結束時它將直接調用onFinish()。有關更多詳細信息,請參閱此源中的Handler方法。

所以,主要的問題是,當我們給一些X(我們的案例10000)毫秒,但開始它採取了一些50至150毫秒的計數器,所以如果我們在我們的總時間添加毫秒,我們將獲取計數器,直到結束,

所以你可以嘗試這樣,沒有什麼改變,只是我總共加了150毫秒。

void startTimer() { 
    cTimer = new CountDownTimer(10150, 1000) { 
     public void onTick(long millisUntilFinished) { 
      c.setText("Please wait " + millisUntilFinished/1000 + " seconds"); 
      thx.setText(millisUntilFinished/1000 + ""); 
      thx.setAlpha(.5f); 
      thx.setClickable(false); 

     } 
     public void onFinish() { 
     c.setText("done"); 
      thx.setText("ready"); 
      thx.setAlpha(1f); 
      thx.setClickable(true); 
     } 
    }; 
    cTimer.start(); 
} 

讓我知道,如果它的工作或沒有,如果你爲什麼不使用處理器問我,我會在內部使用處理器說CountDownTime

+0

正是我想要的。也有道理。 –

+0

@MuthukrishnanRajendran謝謝,我讀了你的答案。但我認爲這不是真的,因爲每個onTick調用它會再吃2-3毫秒。所以,如果你重複足夠長的時間(比如說300或500次),那麼你預先添加的那些150毫秒也不會成功。此外,你的建議只是解決方法,基本確認那裏有一個錯誤。否則...是的,這是一個聰明的解決方法,以增加150毫米的前沿。感謝提示。 –

1

試試這個計數器:

public void CountDown() { 
    final TextView textic = (TextView) findViewById(R.id.tvConuter); 

    CountDownTimer Count = new CountDownTimer(10000, 1000) { 
     public void onTick(long millisUntilFinished) { 
      long str = millisUntilFinished/1000; 
      String TimeFinished = String.valueOf(str); 
      textic.setText(TimeFinished); 
     } 
     public void onFinish() { 
      textic.setText("STOP"); 

     } 
    }; 
    Count.start(); 
} 

這非常適合我。

+0

lol.有什麼區別? – Vyacheslav

0

我建議使用Timer()Handler()類。

int tick = 0; 
    Handler handler = new Handler(); 
    Runnable r = new Runnable() {void run(){ 
if (i < 10) { 
handler.postDelayed()} 
else { 
//finished 
tick = 0; 
}}}; 

首發:

handler.post(r);

0
new CountDownTimer(10000, 1000) { 

public void onTick(long millisUntilFinished) { 
    mTextField.setText("seconds: " + millisUntilFinished/1000); 
    //here you can have your logic to set text to edittext 
} 

public void onFinish() { 
    mTextField.setText("done!"); 
} 

}.start();