Countdown Tool Class

CountDownTimer

If you are in a hurry, just copy the last tool class of the article and paste it into your project.

In the development of APP, we usually have an operation to get the phone verification code. One of the countdown is generally 60s, which is not allowed to be clicked during the countdown period, and then get the verification code after the end of the timer. As shown in the following figure


As shown in the figure, this is the implementation process of countdown. Let's see how to use this tool class.

			@BindView(R.id.get_code)
    		TextView getCode;
    		
			CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(getCode, 60000, 1000);
            countDownTimerUtils.start();

getCode is the name of the control you use to get the verification code in the figure above. 60,000 milliseconds is 60 seconds, 1000 milliseconds is 1 second. The total length of 60 seconds to start countdown is usually button, but I use TextView here. When I click, it counts down, and then the text will become a countdown.

Tool classes are as follows:

public class CountDownTimerUtils extends CountDownTimer {
    WeakReference<TextView> tvCodeWr;//Control Soft Reference to Prevent Memory Leakage
    CountDownTimer timer;

    /**
     * @param textView          The TextView
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receiver
     *                          {@link #onTick(long)} callbacks.
     */
    public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        tvCodeWr = new WeakReference<>(textView);
        timer = this;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        TextView mTextView = tvCodeWr.get();
        if (mTextView != null) {
            mTextView.setClickable(false); //Set Unclickable
            mTextView.setText(millisUntilFinished / 1000 + "second");  //Set countdown time
            //    mTextView.setBackgroundResource(R.drawable.bg_identify_code_press); //Set the button to grey, which is not clickable at this time

            /**
             * Hyperlink URLSpan
             * Text Background ColorSpan
             * Text color ForegroundColorSpan
             * Font size Absolute SizeSpan
             * Bold and italic StyleSpan
             * Delete StrikethroughSpan
             * Underline Span
             * Image Span
             * http://blog.csdn.net/ah200614435/article/details/7914459
             */
            SpannableString spannableString = new SpannableString(mTextView.getText().toString());  //Get the text on the button
            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
            /**
             * public void setSpan(Object what, int start, int end, int flags) {
             * Mainly start and end, start is the starting position, both in English and Chinese.
             * Start at 0. End is the end position, so the text processed contains the start position, but not the end position.
             */
            spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//Set the countdown time to red
            mTextView.setText(spannableString);
        } else {
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    }

    @Override
    public void onFinish() {
        TextView mTextView = tvCodeWr.get();
        if (mTextView != null) {
            mTextView.setText("Get the authentication code");
            mTextView.setClickable(true);//Re-click
        } else {
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    }

}

This tool class can be used directly by copying and pasting, but it needs to import some packages into it, all of which are in As, and there are no third-party packages.

Posted by virtualdevl on Thu, 03 Oct 2019 03:51:12 -0700