2017-11-16 22 views
2

"syncingIntervalsInt"時間を待ってからコードを実行する関数があります。カウントダウンはどのように作成できますか? syncingIntervalsIntが10秒に設定されている場合たとえば、私はこのカウントダウン10,9,8へ1つのTextViewを設定するなどハンドラの残り時間を計算する

ここに私の関数である。syncingIntervalsInt後だけデクリメント上記

public void refreshRecycler() 
{ 
    countingTillSync = syncingIntervalsInt; 

    timerHandler = new Handler(); 

    timerRunnable = new Runnable() { 


     @Override 
     public void run() { 

      //some code here 
      countingTillSync--; 


     } 

     timerHandler.postDelayed(this, syncingIntervalsInt*1000); //run every second 
    } 


    timerHandler.postDelayed(timerRunnable, syncingIntervalsInt*1000); //Start timer after 1 sec 

} 

このコード*私が期待するものではない1000時間。

+0

私の回答を確認できますか? – KeLiuyue

答えて

2

これを試すことができます。このようなコードで

1.Add CountDownTimerUtils

public class CountDownTimerUtils extends CountDownTimer { 

private TextView mTextView; 

/** 
* @param textView   The TextView 
* @param millisInFuture The number of millis in the future from the call 
*       to {@link #start()} until the countdown is done and {@link #onFinish()} 
*       is called. 
* @param countDownInterval The interval along the way to receiver 
*       {@link #onTick(long)} callbacks. 
*/ 
public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) { 
    super(millisInFuture, countDownInterval); 
    this.mTextView = textView; 
} 
@Override 
public void onTick(long millisUntilFinished) { 
    mTextView.setText(millisUntilFinished/1000 + "sec"); 
    mTextView.setClickable(false); 
} 
@Override 
public void onFinish() { 
    mTextView.setText("retry"); 
    mTextView.setClickable(true); 
    mTextView.setFocusable(true); 
} 
} 

2.Set。

// 1 param yourTextView was TextView you want to set 
// 2 param 10 * 1000 was the number of millis in the future from the call 
// 3 param 1000 was the interval along the way to receiver 
CountDownTimerUtils mTimerUtils = new CountDownTimerUtils(yourTextView, 10 * 1000, 1000); 
mTimerUtils.start();