본문 바로가기

Android/Function

[안드로이드 Kotlin] 카운트 다운 타이머(CountdownTimer) 간단 사용하기

728x90
반응형

 

5초 카운트 다운 후 버튼 실행을 해보겠습니다.

  private val mCountDown: CountDownTimer = object : CountDownTimer(5250, 500) {
        override fun onTick(millisUntilFinished: Long) {
            //update the UI with the new count
            counterText.set("${(millisUntilFinished.toFloat() / 1000.0f).roundToInt()}초 뒤에 자동으로 시작 됩니다..")
        }

        override fun onFinish() {
            //countdown finish
            onClickStart()
        }
    }

500ms interval 마다 ui를 업데이트하고,  5250ms(5.25s) 부터 카운트 다운을 시작하도록 하였습니다.

위 코드를 단순 추가한 뒤, 아래 코드로 시작하면 됩니다.

mCountDown.start()

 

  • CountDown 취소
mCountDown.cancle()

 

끝!

 

 

728x90
반응형