註冊時擷取驗證碼常用的倒計時工具,註冊時驗證碼

來源:互聯網
上載者:User

註冊時擷取驗證碼常用的倒計時工具,註冊時驗證碼


//在這個構造方法裡需要傳入三個參數,一個是Activity,一個是總的時間millisInFuture,
// 一個是每次減少的時間,最後一個是顯倒計時的按鈕
用法:
TimeLastUtil timeLastUtil = new TimeLastUtil(this, 60000, 1000, btn);
timeLastUtil.start();


工具類:

public class TimeLastUtil extends CountDownTimer {

private Activity mActivity;
private TextView btn;// 按鈕

//在這個構造方法裡需要傳入三個參數,一個是Activity,一個是總的時間millisInFuture,
// 一個是countDownInterval,然後就是你在哪個按鈕上做這個是,就把這個按鈕傳過來就可以了
public TimeLastUtil(Activity mActivity, long millisInFuture,
long countDownInterval, TextView btn) {
super(millisInFuture, countDownInterval);
this.mActivity = mActivity;
this.btn = btn;
}



@SuppressLint("NewApi")
@Override
public void onFinish() {
btn.setText(R.string.register_regetchecknum);
btn.setClickable(true);// // 重新獲得點擊
btn.setBackgroundColor(Color.parseColor("#00CCCC"));// 還原背景色
}

@Override
public void onTick(long millisUntilFinished) {
btn.setClickable(false);// 設定不能點擊
btn.setText(millisUntilFinished / 1000 + "s後可重新發送");// 設定倒計時時間

// 設定按鈕為灰色,這時是不能點擊的
btn.setBackgroundColor(Color.GRAY);
Spannable span = new SpannableString(btn.getText().toString());// 擷取按鈕的文字
span.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);// 講倒計時時間顯示為紅色
btn.setText(span);

}

}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.