Objective
Now there are a lot of apps with verification Code Countdown button, the implementation of the way there are some of the following:
- 1. Using threads and handler to periodically refresh the countdown numbers, which can easily lead to memory leaks, so generally use weak references to control the number of refreshes.
- 2. Custom Countdown button
- 3, using Android provides Countdowntimer combined with TextView to achieve the countdown function
This article is basically the third way to achieve, easy to use
Realize
Here's the core code:
/** * Created by HFS on 2017/5/9. */PublicClassTimecountExtendsCountdowntimer {Private button button;Private String Ticktext;Private String Finishtext;/** *@param millisinfuture Countdown Total Duration *@param countdowninterval Countdown unit milliseconds. */PublicTimecount (Long Millisinfuture,Long Countdowninterval, Button button,string ticktext,string finishtext) {super (Millisinfuture, countdowninterval); this.button = button; this.ticktext=ticktext; this.finishtext=finishtext;} @Override public void ontick (long millisuntilfinished) {Button.settext ( millisuntilfinished/1000 + ticktext); button.setenabled (false);} @Override public void onfinish () {button.setenabled (true); Button.settext (FinishText);}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
I simply encapsulated myself as a custom button, and then I used a custom button directly
/** * Created by HFS on 2017/5/9. */PublicClassCountdownbuttonExtendsButton {Private Timecount Mtimecount;PrivateLong millisinfuture =60000;PrivateLong Countdowninterval =1000;Private String Mticktext ="s after re-acquisition";Private String Mfinishtext ="Re-acquisition";PublicCountdownbutton (Context context) {Super (context); Init (context); }PublicCountdownbutton (context context, AttributeSet Attrs) {Super (context, attrs); Init (context); }PublicCountdownbutton (context context, AttributeSet attrs,int defstyleattr) {Super (context, attrs, defstyleattr); Init (context); }PrivatevoidInit (Context context) {}/** * Set Countdown to how many milliseconds end * Default 60 seconds * *@param millisinfuture milliseconds */PublicvoidSetmillisinfuture (Long Millisinfuture) {This.millisinfuture = millisinfuture; }/** * Set Countdown interval * Default 1 seconds * *@param countdowninterval Countdown Interval */PublicvoidSetcountdowninterval (Long Countdowninterval) {This.countdowninterval = Countdowninterval; }/** * Set the countdown process button to display the content * @param text default s after retrieving */public void setonticktext (String text) {this.mticktext=text; } /** * Set countdown End button display content * @param finishtext Default regain */public void setonfinishtext (String Finishtext) {this.mfinishtext=finishtext;} public void startCount () { Mtimecount = new timecount (Millisinfuture, Countdowninterval, this, Mticktext, Mfinishtext); Mtimecount.start (); }}
1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- Ten
- one
-
- 2
- (
- )
- +
- +
- /
- 0
-
- +
-
- all
- +
- +
- +
- -
- 29
-
- +
- +
- all
-
- +
- +
- PNS
- up
i>39
-
- 48
- all
-
-
- /
- /
- /
- /li>
-
-
- ,
- ,
- ,
- up-
- -
- +
- -
- +
- *
- +
-
- ,
- ,
-
- +
- $
- "
- "
- "
- "
- "
- " "
- " "
"
- "
"
- "
"
Using the method is simple, just a few lines of code:
Publicclass mainactivity extends appcompatactivity {private Countdownbutton Mcountdownbutton; @Override protected void Span class= "Hljs-title" >oncreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Mcountdownbutton = (Countdownbutton) Findviewbyid (R.id.btn_count_down); Mcountdownbutton.setonclicklistener (new View.onclicklistener () { @Override public void onclick (View v) {mcountdownbutton.startcount ();}); }}
Android Development: Verification Code Countdown function implementation