Android simple package get verification code countdown

We believe that the effect shown in the figure is not unfamiliar to everyone. We can use many methods to achieve this effect. Here we use CountDownTimer timer to simply encapsulate this effect, which is convenient for us to call at any time.

The first page defines the following attributes in attrs.xml:

<resources>
    <declare-styleable name="CountDownButton">
        <attr name="millisinfuture" format="integer"/>
        <attr name="countdowninterva" format="integer"/>
        <attr name="normalColor" format="color"/>
        <attr name="countDownColor" format="color"/>
    </declare-styleable>
</resources>

The following is the specific code of the implementation, a very simple way, easy to understand:

/**
 * Created by xiaolong on 2018/1/12.
 */
@SuppressLint("AppCompatCustomView")
public class CountDownButton extends Button{

    //Total duration
    private long millisinfuture;

    //Interval length
    private long countdowninterva;

    //Default background color
    private int normalColor;

    //Countdown background color
    private int countDownColor;

    //End or not
    private boolean isFinish;

    //timer
    private CountDownTimer countDownTimer;

    public CountDownButton(Context context) {
        this(context,null);
    }

    public CountDownButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CountDownButton,defStyleAttr,0);
        //Set default duration
        millisinfuture = (long) typedArray.getInt(R.styleable.CountDownButton_millisinfuture,60000);
        //Set default interval length
        countdowninterva = (long)typedArray.getInt(R.styleable.CountDownButton_countdowninterva,1000);
        //Set default background color
        normalColor = typedArray.getColor(R.styleable.CountDownButton_normalColor,android.R.color.holo_blue_light);
        //Set default countdown background color
        countDownColor = typedArray.getColor(R.styleable.CountDownButton_countDownColor,android.R.color.darker_gray);
        typedArray.recycle();
        //Default to closed
        isFinish = true;
        //Font Center
        setGravity(Gravity.CENTER);
        //Default text and background colors
        normalBackground();
        //set timer 
        countDownTimer = new CountDownTimer(millisinfuture, countdowninterva) {
            @Override
            public void onTick(long millisUntilFinished) {
                //not finished
                isFinish = false;

                setText((Math.round((double) millisUntilFinished / 1000) - 1) + "second");

                setBackgroundResource(countDownColor);
            }

            @Override
            public void onFinish() {
                //End
                isFinish = true;

                normalBackground();
            }
        };
    }

    private void normalBackground(){
        setText("Get verification code");
        setBackgroundResource(normalColor);
    }

    public boolean isFinish() {
        return isFinish;
    }

    public void cancel(){
        countDownTimer.cancel();
    }

    public void start(){
        countDownTimer.start();
    }

}

A simple way to call:

public class MainActivity extends AppCompatActivity {

    private CountDownButton countDownButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        countDownButton = ((CountDownButton) findViewById(R.id.countDownButton));
        countDownButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Here, judge whether the countdown is over, and avoid repeated request interface caused by multiple clicks during the countdown
                if (countDownButton.isFinish()) {
                    //Call after sending the authentication code successfully.
                    countDownButton.start();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!countDownButton.isFinish()) {
            countDownButton.cancel();
        }
    }
}

Such a simple encapsulation is over, and the process is very simple. This is mainly an exercise on the use of CountDownTimer, which has not been touched before. By the way Source code Now!

Tags: Android xml

Posted on Fri, 01 May 2020 16:08:39 -0700 by thunder708