Android使用CountDownTimer模擬短信驗(yàn)證倒計時
本文為大家分享了CountDownTimer模擬短信驗(yàn)證倒計時的具體代碼,供大家參考,具體內(nèi)容如下
內(nèi)容:介紹倒計時CountDownTimer的基本使用方法。模擬短信驗(yàn)證
步驟:
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="獲取短信驗(yàn)證碼" 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("獲取短信驗(yàn)證碼"); } }
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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中TextView顯示圓圈背景或設(shè)置圓角的方法
TextView顯示文本給用戶,并允許他們選擇編輯。TextView是一個完整的文本編輯器,但是其基本類配置為不允許編輯。下面這篇文章主要給大家介紹了關(guān)于Android中TextView顯示圓圈背景或設(shè)置圓角的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對象的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)全局獲取Context及使用Intent傳遞對象的方法,結(jié)合實(shí)例形式分析了Android全局Context的獲取及Intent傳遞對象的具體操作方法,需要的朋友可以參考下2017-08-08Android Jetpack組件中LiveData的優(yōu)劣
LiveData是Jetpack組件的一部分,更多的時候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件(?Activity、Fragment、Service)處于活動生命周期狀態(tài)的時候才會更新數(shù)據(jù)2023-04-04android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例
本篇文章主要介紹了android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Android自定義控件實(shí)現(xiàn)九宮格解鎖
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)九宮格解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06Android框架Volley使用之Json請求實(shí)現(xiàn)
這篇文章主要介紹了Android框架Volley使用之Json請求實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05