1. 程式人生 > >TextView特效顯示 獲取驗證碼倒計時

TextView特效顯示 獲取驗證碼倒計時

 SpannableStringBuilder spannableString = new SpannableStringBuilder();
                String name = "這是一個暱稱:";
                spannableString.append(name);
                spannableString.append(s.toString());
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#fe0072"));
                spannableString.setSpan(colorSpan, 0, name.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                BackgroundColorSpan bgColorSpan = new BackgroundColorSpan(Color.parseColor("#009ad6"));
                spannableString.setSpan(bgColorSpan, 0, name.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                tv.setText(spannableString); 

TextView設定一段文字展示不同的顏色,背景,字型大小

獲取驗證碼的倒計時

    /**
     * 2018/7/4/004 10:50
     * 驗證碼倒計時
     */
    private CountDownTimer countDownTimer = new CountDownTimer(120000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            long time = millisUntilFinished / 1000;
            tvCode.setText(time + "秒後重新獲取");
            tvCode.setClickable(false);
        }

        @Override
        public void onFinish() {
            tvCode.setText("重新獲取驗證碼");
            tvCode.setClickable(true);
        }
    };

使用

        //開始倒計時
        countDownTimer.start();

銷燬(頁面銷燬時一定記得把倒計時清理掉)


    /* 頁面銷燬前的清理工作 */
    @Override
    protected void onDestroy() {
        //銷燬計時器
        countDownTimer.cancel();
        countDownTimer.onFinish();
        countDownTimer = null;
        super.onDestroy();
    }