Android custom view get registration verification code countdown button

In Android development, we will inevitably achieve the registration function, and most of the current registrations are registered with mobile phones, so it will be required to obtain the verification code when registering. Next, let's practice the custom countdown button for obtaining the verification code:

1. Look at the renderings first

2. Variables involved

3. What countdown timer is used

In Java, the execution of timer task requires two basic classes: Java. Util. Timer; java.util.TimerTask;

To run a scheduled task, the basic steps are as follows: 1. Create a task to be executed, TimerTask. 2. Create a timer instance, add TimerTask to the timer through the schedule () method provided by timer, and set the execution rules at the same time.

When the program executes the timer initialization code, the timer timing task will be executed according to the settings. The schedule () method in timer has multiple overloaded formats to adapt to different situations. The format of this method is as follows: void schedule (TimerTask task, date time) schedules the execution of the specified task at the specified time. Void schedule (TimerTask task, date, firsttime, long period) schedules the specified task to start repeated fixed delay execution at the specified time. Void schedule (TimerTask task, long delay) schedules the execution of a specified task after a specified delay. Void schedule (TimerTask task, long delay, long period) schedules the specified task to execute repeatedly with a fixed delay after the specified delay. Timer is thread safe. This class can be extended to a large number of tasks scheduled at the same time (there are thousands of tasks without problems). All of its constructors start the timer thread. You can call cancel() to terminate this timer and discard all currently scheduled tasks. Purge() removes all cancelled tasks from the timer's task queue. This class does not provide real-time assurance: it uses the object. Wait (long) method to schedule tasks. TimerTask is an abstract class, which is scheduled by timer to be executed once or repeatedly. It has an abstract method run () -- the operation to be performed by the timer task. Therefore, each concrete task class must inherit the TimerTask class and override the run () method. In addition, it has two non abstract methods: Boolean cancel() to cancel the timer task. Long scheduledexecutiontime() returns the scheduled execution time of the latest actual execution of this task.

4. Code

5. Usage, super simple

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>