android中倒計時功能

android中的倒計時功能,主要用於發送驗證碼以及其他。

我在這個項目裏用於打卡。

 //讀卡倒計時邏輯
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        public void onFinish() {
            bt_get_cart_id.setText("點擊讀卡");
            txtCode.setText("讀卡超時 請重試");
            bt_get_cart_id.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(runnable).start();//打卡用子線程
                    time.start();
                }
            });
            ColseCom();
        }

        public void onTick(long millisUntilFinished) {
            bt_get_cart_id.setText("讀卡中");
            bt_get_cart_id.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bt_get_cart_id.setText("點擊讀卡");
                    bt_get_cart_id.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            new Thread(runnable).start();//打卡用子線程
                            time.start();
                        }
                    });
                    ColseCom();
                    time.cancel();
                }
            });
        }
    }

代碼有點亂,只能作爲參考。

用法:

TimeCount time = new TimeCount(6000,1000);//倒計時六秒


發佈了29 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章