Android獲取驗證碼倒計時實現(xiàn)代碼
本文實例為大家分享了Android獲取驗證碼倒計時的具體代碼,供大家參考,具體內(nèi)容如下
1. 驗證碼輸入框和獲取驗證碼按鈕布局
xml代碼:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="horizontal" >
<EditText
android:id="@+id/phonetext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_gravity="center_vertical"
android:inputType="number"
android:hint="請輸入短信驗證碼"
android:background="@null"/>
<Button
android:id="@+id/timebutton"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:textSize="16dp"
android:background="@drawable/tv_timemessage_bg"
android:text="獲取"
/>
</LinearLayout>效果如下:

2. 根據(jù)id設(shè)置Button點擊事件觸發(fā)倒計時
JAVA代碼:
/**
* Created by fby on 2017/9/11.
*/
public class ChargepsdActivity extends Activity {
private Button timeButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chargepsd);
timeButton = (Button) findViewById(R.id.timebutton);
//new倒計時對象,總共的時間,每隔多少秒更新一次時間
final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);
//設(shè)置Button點擊事件觸發(fā)倒計時
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myCountDownTimer.start();
}
});
}3. 倒計時函數(shù)
//倒計時函數(shù)
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//計時過程
@Override
public void onTick(long l) {
//防止計時過程中重復(fù)點擊
timeButton.setClickable(false);
timeButton.setText(l/1000+"秒");
}
//計時完畢的方法
@Override
public void onFinish() {
//重新給Button設(shè)置文字
timeButton.setText("重新獲取");
//設(shè)置可點擊
timeButton.setClickable(true);
}
}
}4. 清除倒計時函數(shù),解決驗證碼輸入正確后停止計時
private void clearTimer() {
if (task != null) {
task.cancel();
task = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android賬號注冊實現(xiàn)點擊獲取驗證碼倒計時效果
- Android實現(xiàn)點擊獲取驗證碼倒計時效果
- Android開發(fā)之獲取短信驗證碼后按鈕背景變化并且出現(xiàn)倒計時
- Android實現(xiàn)發(fā)送短信驗證碼倒計時功能示例
- Android短信驗證碼倒計時驗證的2種常用方式
- Android利用CountDownTimer實現(xiàn)驗證碼倒計時效果實例
- Android自定義View獲取注冊驗證碼倒計時按鈕
- Android獲取驗證碼倒計時顯示效果
- Android使用Kotlin和RxJava 2.×實現(xiàn)短信驗證碼倒計時效果
- Android 用RxBinding與RxJava2實現(xiàn)短信驗證碼倒計時功能
相關(guān)文章
AndroidHttpClient詳解及調(diào)用示例
本文給大家介紹AndroidHttpClient結(jié)構(gòu)、使用方式及調(diào)用示例詳解,需要的朋友可以參考下2015-10-10
Android使用CardView作為RecyclerView的Item并實現(xiàn)拖拽和左滑刪除
這篇文章主要介紹了Android使用CardView作為RecyclerView的Item并實現(xiàn)拖拽和左滑刪除,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android分頁中顯示出下面翻頁的導(dǎo)航欄的布局實例代碼
這篇文章主要介紹了Android分頁中顯示出下面翻頁的導(dǎo)航欄的布局實例代碼,需要的朋友可以參考下2017-04-04
Android Compose衰減動畫Animatable使用詳解
這篇文章主要為大家介紹了Android Compose衰減動畫Animatable使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android自定義View 仿QQ側(cè)滑菜單的實現(xiàn)代碼
這篇文章主要介紹了Android自定義View 仿QQ側(cè)滑菜單的實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-08-08
Android字符串和十六進制相互轉(zhuǎn)化出現(xiàn)的中文亂碼問題
這篇文章主要介紹了Android字符串和十六進制相互轉(zhuǎn)化出現(xiàn)的中文亂碼問題的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android Activity切換(跳轉(zhuǎn))時出現(xiàn)黑屏的解決方法 分享
Android Activity切換(跳轉(zhuǎn))時出現(xiàn)黑屏的解決方法 分享,需要的朋友可以參考一下2013-06-06

