Android倒計時實現

來源:互聯網
上載者:User

標籤:抽象類別   count   format   倒計時   art   任務   height   str   ext   

Android為我們封裝好了一個抽象類別CountDownTimer,可以實現計時器功能:

 

   /**     * 倒數計時器     */    private CountDownTimer timer = new CountDownTimer(15 * 60 * 1000, 1000) {        /**         * 固定間隔被調用,就是每隔countDownInterval會回調一次方法onTick         * @param millisUntilFinished         */        @Override        public void onTick(long millisUntilFinished) {            tv_remaining_time.setText(formatTime(millisUntilFinished));        }        /**         * 倒計時完成時被調用         */        @Override        public void onFinish() {            tv_remaining_time.setText("00:00");        }    };    /**     * 將毫秒轉化為 分鐘:秒 的格式     *     * @param millisecond 毫秒     * @return     */    public String formatTime(long millisecond) {        int minute;//分鐘        int second;//秒數        minute = (int) ((millisecond / 1000) / 60);        second = (int) ((millisecond / 1000) % 60);        if (minute < 10) {            if (second < 10) {                return "0" + minute + ":" + "0" + second;            } else {                return "0" + minute + ":" + second;            }        }else {            if (second < 10) {                return minute + ":" + "0" + second;            } else {                return minute + ":" + second;            }        }    }    /**     * 取消倒計時     */    public void timerCancel() {        timer.cancel();    }    /**     * 開始倒計時     */    public void timerStart() {        timer.start();    }

 

構造方法裡需要傳入兩個參數進去:

參數1:倒計時的總時間,單位ms

參數2:倒計時的時間間隔,單位ms

 

方法:

cancel():取消倒計時

onFinish():當前任務完成的時候回調

onTick(long millisUnitilFinished):當前任務每完成一次倒計時間隔時間時回調

start():開始倒計時

 

      

 

Android倒計時實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.