簡訊註冊倒計時功能

來源:互聯網
上載者:User

標籤:android   style   blog   http   os   ar   java   for   sp   

android中都有註冊的功能,現在很多註冊的都是用手機號,因為用手機號註冊,app可以根據註冊的手機號推送一些資料給用戶端,為了防止有的使用者頻繁的去擷取驗證碼,可以用時間做一些操作防止此情況的發生,比如使用者只能一分鐘才能點擊,否則不讓它點擊,在一分鐘之內的讓按鈕不能被點擊,一分鐘之後才讓使用者可以點擊, 當使用者輸入手機號,擷取驗證碼的過程中,有一個倒計時的功能,今天就講講這個實現,其實在android中就已經為我們提供了此類方便操作 在android.os.CountDownTimer包中,


執行個體代碼:

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){//當使用者輸入的手機不為11位時,就不能點擊擷取驗證碼的按鈕 btn_coder.setClickable(true); }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,1000) {@Overridepublic void onTick(long millisUntilFinished) {btn_coder.setText(millisUntilFinished/1000+"");btn_coder.setClickable(false);//防止重複點擊Log.e("CountDownTimer","millisUntilFinished="+millisUntilFinished/1000);}@Overridepublic void onFinish() {//可以在這做一些操作,如果沒有擷取到驗證碼再去請求伺服器btn_coder.setClickable(true);//防止重複點擊btn_coder.setText("擷取驗證碼");}};countDownTimer.start();}}

還可以通過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("擷取驗證碼");                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){//當使用者輸入的手機不為11位時,就不能點擊擷取驗證碼的按鈕 btn_coder.setClickable(true); }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);//延遲一秒執行          //在此調簡訊的介面,}}




簡訊註冊倒計時功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.