Andorid realizes the countdown effect of clicking to obtain the verification code
We often use the countdown function in development. For example, after sending the verification code, count down for 60s and then obtain the verification code. In order to facilitate future use, let's make a record and talk about the implementation of the countdown timer.
1. Encapsulation of advanced countdown tool class
Let this tool class inherit countdowntimer, and then pass in the view showing the countdown. During the countdown, ontick method will be called to refresh the countdown interface of view. After the countdown, onfinish method will be called to restore the state of view.
2. Layout file
The view layout shape when the get verification code is not clicked_ verify_ btn_ normal. xml
Click the view layout shape after obtaining the verification code_ verify_ btn_ press. xml
Layout of the whole interface
There is a problem with the layout here, because the word in the textview of obtaining the verification code will change during the countdown, and the change length of the word is different each time. If its layout_ Set width to wrap_ Content, the length of this textview will change, affecting the appearance of the interface, so you can fix its length, and then enter the verification code into the layout of the box_ Set weight to 1, which is OK.
3. Specific use method
60000 represents the countdown time, i.e. 60s; 1000 means decreasing by 1s each time.
The above is the specific implementation process, and several renderings are attached below!
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.