Simple encapsulation of the 60-second countdown Button for Android to get the verification code, Android button
Since it is troublesome to use a countdown button when registering to obtain the verification code, I wrote a TimeButton.
Mouse and mouse features,
TimeButton does not conflict with common buttons,
TimeButton returns the result in the countdown. If the time does not exceed the remaining time, it will continue to run,
In TimeButton, the button click display and countdown logic are completed,
The TimeButton will also be destroyed after the activity is destroyed and will not run in the background like a thread,
The display text and countdown of TimeButton can be set by yourself. Of course, the default value is also available,
Original address http://write.blog.csdn.net/postedit/43563033
To paste the code, you must first define a Map in the Application
Public class App extends Application {// used to store the countdown time public static Map <String, Long> map ;}
And then use
Package com. yung. timebutton; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. toast;/*** test main interface ** @ author yung * <P> * January 14, 2015 13:00:26 */public class MainActivity extends Activity implements OnClickListener {private TimeButton v; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); v = (TimeButton) findViewById (R. id. button1); v. onCreate (savedInstanceState); v. setTextAfter ("Get again in seconds "). setTextBefore ("click to obtain Verification Code "). setLenght (15*1000); v. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "this is processing the caller onclicklistnenr", Toast. LENGTH_SHORT ). show () ;}@ Overrideprotected void onDestroy () {// TODO Auto-generated method stubv. onDestroy (); super. onDestroy ();}}
The following is the TimeButton code.
Package com. yung. timebutton; import java. util. hashMap; import java. util. map; import java. util. timer; import java. util. timerTask; import android. annotation. suppressLint; import android. content. context; import android. OS. bundle; import android. OS. handler; import android. util. attributeSet; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button ;/** * In view of the frequent use of the countdown button for obtaining the verification code, I cannot find the ideal one on the Internet. I wrote a *** @ author yung * <P> * January 14, 2015 [Buddha bless never had a BUG] * <p> * PS: since it is found that the schedule method cannot be re-executed after each timer cancle (), it is only possible that the timer is completed. * re-set timer every time you start timing. I didn't expect a good solution for the first time * pay attention to the onCreate () onDestroy () of this class and the onCreate () onDestroy () of the activity () synchronous processing **/public class TimeButton extends Button implements OnClickListener {private long lenght = 60*1000; // countdown length. The default 60 seconds private String textafter = "second suffix Newly acquired ~ "; Private String textbefore =" click to obtain the verification code ~ "; Private final String TIME =" time "; private final String CTIME =" ctime "; private OnClickListener mOnclickListener; private Timer t; private TimerTask tt; private long time; Map <String, long> map = new HashMap <String, Long> (); public TimeButton (Context context) {super (context); setOnClickListener (this);} public TimeButton (Context context, attributeSet attrs) {super (context, attrs); setOnClickListener (this) ;} @ SuppressLint ("HandlerLeak") Handler han = new Handler () {public void handleMessage (android. OS. message msg) {TimeButton. this. setText (time/1000 + textafter); time-= 1000; if (time <0) {TimeButton. this. setEnabled (true); TimeButton. this. setText (textbefore); clearTimer () ;};}; private void initTimer () {time = lenght; t = new Timer (); tt = new TimerTask () {@ Overridepublic void run () {Log. e ("yung", time/ 1000 + ""); han. sendEmptyMessage (0x01) ;}};} private void clearTimer () {if (tt! = Null) {tt. cancel (); tt = null;} if (t! = Null) t. cancel (); t = null ;}@ Overridepublic void setOnClickListener (OnClickListener l) {if (l instanceof TimeButton) {super. setOnClickListener (l);} elsethis. mOnclickListener = l ;}@ Overridepublic void onClick (View v) {if (mOnclickListener! = Null) mOnclickListener. onClick (v); initTimer (); this. setText (time/1000 + textafter); this. setEnabled (false); t. schedule (tt, 0, 1000); // t. scheduleAtFixedRate (task, delay, period);}/*** synchronize with the onDestroy () method of the activity */public void onDestroy () {if (App. map = null) App. map = new HashMap <String, Long> (); App. map. put (TIME, time); App. map. put (CTIME, System. currentTimeMillis (); clearTimer (); Log. e ("yung", "onDestroy");}/*** synchronize with the onCreate () method of the activity */public void onCreate (Bundle bundle) {Log. e ("yung", App. map + ""); if (App. map = null) return; if (App. map. size () <= 0) // The returned time is not completed last time; long time = System. currentTimeMillis ()-App. map. get (CTIME)-App. map. get (TIME); App. map. clear (); if (time> 0) return; else {initTimer (); this. time = Math. abs (time); t. schedule (tt, zero, 1000); this. setText (time + textafter); this. setEnabled (false) ;}}/*** set the text displayed during timing */public TimeButton setTextAfter (String text1) {this. textafter = text1; return this;}/*** click the previous text */public TimeButton setTextBefore (String text0) {this. textbefore = text0; this. setText (textbefore); return this;}/*** set the time length ** @ param lenght * default time millisecond * @ return */public TimeButton setLenght (long lenght) {this. lenght = lenght; return this ;}/***/}
The Code is a bit bad. Please complete the project to be uploaded.
Project address
Http://download.csdn.net/detail/yung7086/8428517
Original address
Http://write.blog.csdn.net/postedit/43563033