国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現發送短信驗證碼倒計時功能示例

2019-12-12 03:19:44
字體:
來源:轉載
供稿:網友

一、簡介:

開發中在用戶注冊或找回密碼之類的功能,經常會遇到獲取短信驗證碼,獲取驗證碼后需要等待1分鐘倒計時,這段時間是不能再次發送短信請求的。

效果圖:


二、實現步驟:

1、一個關鍵類:CountDownTimer(Android系統自帶的倒計時功能類)

public class CountDownTimerUtils extends CountDownTimer {  private TextView mTextView; //顯示倒計時的文字  /**   * @param textView     The TextView   * @param millisInFuture   millisInFuture 從開始調用start()到倒計時完成   *              并onFinish()方法被調用的毫秒數。(譯者注:倒計時時間,單位毫秒)   * @param countDownInterval 接收onTick(long)回調的間隔時間。(譯者注:單位毫秒)   */  public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {    super(millisInFuture, countDownInterval);    this.mTextView = textView;  }  @Override  public void onTick(long millisUntilFinished) {    mTextView.setClickable(false); //設置不可點擊    mTextView.setText(millisUntilFinished / 1000 + "秒后可重新發送"); //設置倒計時時間    mTextView.setBackgroundResource(R.drawable.validate_code_press_bg); //設置按鈕為灰色,這時是不能點擊的    /**     * 超鏈接 URLSpan     * 文字背景顏色 BackgroundColorSpan     * 文字顏色 ForegroundColorSpan     * 字體大小 AbsoluteSizeSpan     * 粗體、斜體 StyleSpan     * 刪除線 StrikethroughSpan     * 下劃線 UnderlineSpan     * 圖片 ImageSpan     */    SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //獲取按鈕上的文字    ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);    /**     * public void setSpan(Object what, int start, int end, int flags) {     * 主要是start跟end,start是起始位置,無論中英文,都算一個。     * 從0開始計算起。end是結束位置,所以處理的文字,包含開始位置,但不包含結束位置。     */    spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//將倒計時的時間設置為紅色    mTextView.setText(spannableString);  }  @Override  public void onFinish() {    mTextView.setText("重新獲取驗證碼");    mTextView.setClickable(true);//重新獲得點擊    mTextView.setBackgroundResource(R.drawable.validate_code_normal_bg); //還原背景色  }}

2、在合適的地方調用

使用:

CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mButton, 60000, 1000); //倒計時1分鐘mCountDownTimerUtils.start();

3、validate_code_press_bg.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">  <!--發送短信驗證碼按鈕被按下-->  <solid android:color="#C0C0C0" /> <!--填充色 透明-->  <corners android:radius="8dp" /> <!-- 圓角 --></shape>

validate_code_normal_bg.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">  <!--發送短信驗證碼按鈕未按下-->  <solid android:color="#FF9933" /> <!--填充色 透明-->  <corners android:radius="8dp" /> <!-- 圓角 --></shape>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张家港市| 英山县| 四川省| 富裕县| 晴隆县| 黑龙江省| 申扎县| 邯郸市| 涞水县| 神池县| 永修县| 邛崃市| 杭锦后旗| 饶河县| 丰原市| 宿松县| 新河县| 富蕴县| 宜兰县| 潮州市| 保德县| 武宁县| 封开县| 兰坪| 桐城市| 辽宁省| 延安市| 岚皋县| 南陵县| 涿州市| 吕梁市| 泽州县| 青田县| 乌鲁木齐县| 安化县| 娄烦县| 余姚市| 新泰市| 祁东县| 新竹县| 荔波县|