Android使用CountDownTimer模擬短信驗證倒計時
本文為大家分享了CountDownTimer模擬短信驗證倒計時的具體代碼,供大家參考,具體內(nèi)容如下

內(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)歌詞漸變色和進(jìn)度的效果
這篇文章主要介紹了Android實現(xiàn)歌詞漸變色和進(jìn)度的效果的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android統(tǒng)一依賴管理的三種方式總結(jié)
為了項目的管理,依賴包的紡一管理是必要的,下面這篇文章主要給大家介紹了關(guān)于Android統(tǒng)一依賴管理的三種方式,文中通過實例代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
深入Android中BroadcastReceiver的兩種注冊方式(靜態(tài)和動態(tài))詳解
這篇文章主要介紹了深入Android中BroadcastReceiver的兩種注冊方式(靜態(tài)和動態(tài))詳解,具有一定的參考價值,有需要的可以了解一下。2016-12-12
Android中控件GridView實現(xiàn)設(shè)置行列分割線的方法示例
這篇文章主要介紹了利用Android中控件GridView實現(xiàn)設(shè)置行列分割線的方法,文中給出了詳細(xì)的介紹與示例代碼,相信對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。2017-01-01
Android中使用socket使底層和framework通信的實現(xiàn)方法
native和framework的通信是通過jni,但是這一般只是framework調(diào)用native,native如果有消息要怎樣通知上層 呢?android中GSP模塊提供一種解決思路,但是實現(xiàn)有些復(fù)雜,這里介紹一種使用socket通信的方法可以使native和framework自由通信,感興趣的朋友一起看看吧2016-11-11
Android WebView調(diào)用本地相冊的方法
這篇文章主要為大家詳細(xì)介紹了Android WebView調(diào)用本地相冊的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

