本文為大家分享了CountDownTimer模擬短信驗證倒計時的具體代碼,供大家參考,具體內(nèi)容如下
站在用戶的角度思考問題,與客戶深入溝通,找到宜黃網(wǎng)站設(shè)計與宜黃網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋宜黃地區(qū)。
內(nèi)容:介紹倒計時CountDownTimer的基本使用方法。模擬短信驗證
步驟:
1、繼承CountDownTimer,重寫onTick()、onFinish()
2、代碼中new出CountDownTimer子類,傳好參數(shù),調(diào)用start()執(zhí)行
代碼如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.leixiansheng.countdowntimer.MainActivity"> <TextView android:id="@+id/tv_getMsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="獲取短信驗證碼" android:background="@color/colorPrimaryDark" android:textSize="16sp" android:textColor="#ffffffff" /> </RelativeLayout>
TimerCount
package com.example.leixiansheng.countdowntimer; import android.os.CountDownTimer; import android.widget.TextView; /** * Created by Leixiansheng on 2018/7/18. */ public class TimerCount extends CountDownTimer { private TextView mTextView; public TimerCount(long millisInFuture, long countDownInterval, TextView textView) { super(millisInFuture, countDownInterval); mTextView = textView; } @Override public void onTick(long millisUntilFinished) { mTextView.setClickable(false); mTextView.setText("重新獲取" + millisUntilFinished / 1000 + "秒"); } @Override public void onFinish() { mTextView.setClickable(true); mTextView.setText("獲取短信驗證碼"); } }
Main
package com.example.leixiansheng.countdowntimer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView textView = (TextView) findViewById(R.id.tv_getMsg); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** * millisInFuture:要計數(shù)的總時長 * countDownInterval:每隔多少秒響應(yīng) */ TimerCount timerCount = new TimerCount(5000, 1000, textView); timerCount.start(); } }); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當前文章:Android使用CountDownTimer模擬短信驗證倒計時
文章轉(zhuǎn)載:http://chinadenli.net/article8/pddhop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、移動網(wǎng)站建設(shè)、建站公司、ChatGPT、網(wǎng)站改版、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)