In the Android APP's mobile phone number registration logic, there will often be the activation code sent to the mobile phone link, the majority of applications in this link to take into account the network delay or server pressure, as well as the delay of the SMS service providers, will give the user a re-get Activation code button. As shown in the following:
Similarly, in order to prevent users from malicious frequent sending activation code, the application needs to send the user activation code time interval limit, then need to use the countdown device, the process is like this: when the page is initialized, the button is clickable state, the user clicked "Send Activation Code" button to become non-clickable state, At the same time the text on the button becomes a countdown, and after the countdown, the button becomes clickable and the text becomes "Send activation Code". The specific logic looks at the following code:
Package Cn.sunzn.countdown;import Android.app.activity;import Android.os.bundle;import Android.os.CountDownTimer; Import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity implements Onclicklistener {private Timecount Timecou nt Private Button Btn_reget_captcha; protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Init (); } private void Init () {Initview (); InitData (); } private void InitData () {timecount = new Timecount (60000, 1000); } private void Initview () {Btn_reget_captcha = (Button) Findviewbyid (R.id.btn_reget_captcha); Btn_reget_captcha.setonclicklistener (this); public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } public void OnClick (View v) {int id = V.getid (); Switch (ID) {Case r.id.btn_reget_captcha:if (btn_reget_captcha.isclickable ())} {//TODO Run your logic that you want to do Timecount.start (); } break; Default:break; }} class Timecount extends Countdowntimer {public Timecount (long millisinfuture, long Countdowninterval) { Super (Millisinfuture, countdowninterval); } public void OnFinish () {btn_reget_captcha.settext ("Send Activation Code"); Btn_reget_captcha.setclickable (TRUE); public void OnTick (long millisuntilfinished) {btn_reget_captcha.setclickable (false); Btn_reget_captcha.settext ("Click on the re-send activation code after" + millisuntilfinished/1000 + "seconds); } }}
Finally, attach the engineering code: Countdown
Timer under Android programming