2017-11-16 15 views
2

"syncingIntervalsInt"시간을 대기 한 다음 코드를 수행하는 함수가 있습니다. 어떻게 카운트 다운을 만들 수 있습니까? * 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 

} 

이 코드 : syncingIntervalsInt 10 초로 설정 예를 들어, 나는이 카운트 다운 10,9,8 등이 여기에

하나의 텍스트 뷰를 설정하고자하는 것은 내 기능입니다 내가 기대하는 바가 아닌 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..

// 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();