Text message registration countdown Function

Source: Internet
Author: User

Text message registration countdown Function

Android has the registration function. Currently, many registered users use mobile phone numbers. Because mobile phone numbers are used for registration, the app can push some data to the client based on the registered mobile phone number, in order to prevent some users from frequently obtaining verification codes, you can use time to do some operations to prevent this situation. For example, the user can only click in one minute; otherwise, the user is not allowed to click, the button cannot be clicked within one minute, and the user can click it after one minute. When the user enters the mobile phone number and obtains the verification code, there is a countdown function. Let's talk about this implementation today, in fact, this kind of convenient operation has been provided for us in android. OS. countDownTimer package,


Instance code:

Public class MainActivity extends Activity {private EditText et_phone; private Button btn_coder; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_phone = (EditText) findViewById (R. id. et_phone); btn_coder = (Button) findViewById (R. id. btn_coder); et_phone.addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {if (s. toString (). length () = 11) {// when the phone number entered by the user is not 11 characters, you cannot click the "btn_coder.setClickable (true)" button to obtain the verification code );} else {btn_coder.setClickable (false) ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic void afterTextChanged (Editable s) {}}); btn_coder.setOnClickListener (new OnClickListener () {public void onClick (View v) {startTimer () ;}});} protected void startTimer () {CountDownTimer countDownTimer = new CountDownTimer (60*1000) {@ Overridepublic void onTick (long millisUntilFinished) {interval (millisUntilFinished/+ ""); reset (false ); // prevent repeated Log clicks. e ("CountDownTimer", "millisUntilFinished =" + millisUntilFinished/1000);} @ Overridepublic void onFinish () {// you can perform some operations here, if no verification code is obtained, request the server btn_coder.setClickable (true); // prevent repeated btn_coder.setText ("Get Verification Code"); }}; countDownTimer. start ();}}

You can also use Handler

Public class MainActivity extends Activity {private EditText et_phone; private Button btn_coder; private int mCount = 60; private Handler mHandler = new Handler (); Runnable runnable = new Runnable () {@ Override public void run () {mCount --; if (mCount> 0) {btn_coder.setText ("" + mCount); mHandler. postDelayed (this, 1000);} else {btn_coder.setText ("Get Verification Code"); mHandler. removeCallbacks (this) ;}}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_phone = (EditText) findViewById (R. id. et_phone); btn_coder = (Button) findViewById (R. id. btn_coder); et_phone.addTextChangedListener (new TextWatcher () {@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {if (s. toString (). length () = 11) {// when the phone number entered by the user is not 11 characters, you cannot click the "btn_coder.setClickable (true)" button to obtain the verification code );} else {btn_coder.setClickable (false) ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic void afterTextChanged (Editable s) {}}); btn_coder.setOnClickListener (new OnClickListener () {public void onClick (View v) {handlerDown () ;}});} protected void handlerDown () {mCount = 60; mHandler. postDelayed (runnable, 1000); // an API that calls a text message in one second ,}}


Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.