Android實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
我們?cè)陂_(kāi)發(fā)中經(jīng)常用到倒計(jì)時(shí)的功能,比如發(fā)送驗(yàn)證碼后,倒計(jì)時(shí)60s再進(jìn)行驗(yàn)證碼的獲取,為了方便以后使用,這里做個(gè)記錄,講講倒計(jì)時(shí)器的實(shí)現(xiàn)。
1、先進(jìn)行倒計(jì)時(shí)工具類(lèi)的封裝
public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param textView The TextView * * * @param millisInFuture The number of millis in the future from the call * to {@link #start()} until the countdown is done and {@link #onFinish()} * is called. * @param countDownInterval The interval along the way to receiver * {@link #onTick(long)} callbacks. */ public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); this.mTextView = textView; } /** * 倒計(jì)時(shí)期間會(huì)調(diào)用 * @param millisUntilFinished */ @Override public void onTick(long millisUntilFinished) { mTextView.setClickable(false); //設(shè)置不可點(diǎn)擊 mTextView.setText(millisUntilFinished / 1000 + "秒"); //設(shè)置倒計(jì)時(shí)時(shí)間 mTextView.setBackgroundResource(R.drawable.shape_verify_btn_press); //設(shè)置按鈕為灰色,這時(shí)是不能點(diǎn)擊的 SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //獲取按鈕上的文字 ForegroundColorSpan span = new ForegroundColorSpan(Color.RED); /** * public void setSpan(Object what, int start, int end, int flags) { * 主要是start跟end,start是起始位置,無(wú)論中英文,都算一個(gè)。 * 從0開(kāi)始計(jì)算起。end是結(jié)束位置,所以處理的文字,包含開(kāi)始位置,但不包含結(jié)束位置。 */ spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//將倒計(jì)時(shí)的時(shí)間設(shè)置為紅色 mTextView.setText(spannableString); } /** * 倒計(jì)時(shí)完成后調(diào)用 */ @Override public void onFinish() { mTextView.setText("重新獲取驗(yàn)證碼"); mTextView.setClickable(true);//重新獲得點(diǎn)擊 mTextView.setBackgroundResource(R.drawable.shape_verify_btn_normal); //還原背景色 } }
讓這個(gè)工具類(lèi)繼承CountDownTimer,然后把顯示倒計(jì)時(shí)的View傳進(jìn)去,在倒計(jì)時(shí)期間會(huì)調(diào)用onTick方法,在這個(gè)方法里面對(duì)View的倒計(jì)時(shí)界面進(jìn)行刷新,倒計(jì)時(shí)結(jié)束后,會(huì)調(diào)用onFinish方法,在里面恢復(fù)View的狀態(tài)即可。
2、布局文件
未點(diǎn)擊獲取驗(yàn)證碼時(shí)的view布局 shape_verify_btn_normal.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 填充的顏色:這里設(shè)置背景透明 --> <solid android:color="@color/maincolor" /> <!-- 邊框的顏色 :不能和窗口背景色一樣--> <stroke android:width="1dp" android:color="@color/white" /> <!-- 設(shè)置按鈕的四個(gè)角為弧形 --> <!-- android:radius 弧形的半徑 --> <corners android:radius="5dip" /> <!-- padding:Button里面的文字與Button邊界的間隔 --> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape>
點(diǎn)擊獲取驗(yàn)證碼之后的view布局 shape_verify_btn_press.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 填充的顏色:這里設(shè)置背景透明 --> <solid android:color="@color/graywhite" /> <!-- 邊框的顏色 :不能和窗口背景色一樣--> <stroke android:width="1dp" android:color="@color/white" /> <!-- 設(shè)置按鈕的四個(gè)角為弧形 --> <!-- android:radius 弧形的半徑 --> <corners android:radius="5dip" /> <!-- padding:Button里面的文字與Button邊界的間隔 --> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> </shape>
整個(gè)界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/login_bg"> <EditText android:id="@+id/rst_phone_number" android:layout_width="match_parent" android:layout_height="40dp" android:inputType="phone" android:hint="@string/phone_number" android:textSize="16sp" android:textColor="@color/black" android:singleLine="true" android:background="@drawable/shape_form" android:textCursorDrawable="@drawable/text_cursor" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:layout_marginTop="30dp" android:layout_gravity="center_vertical"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:layout_marginTop="15dp" android:orientation="horizontal"> <EditText android:id="@+id/rst_verify_code" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:inputType="number" android:hint="@string/rst_verify_code" android:textSize="16sp" android:textColor="@color/black" android:singleLine="true" android:background="@drawable/shape_form" android:textCursorDrawable="@drawable/text_cursor" /> <TextView android:id="@+id/rst_send_code" android:layout_width="130dp" android:layout_height="match_parent" android:text="@string/rst_send_code" android:textSize="13sp" android:textColor="@color/white" android:gravity="center" android:layout_marginLeft="10dp" android:background="@drawable/shape_verify_btn_normal"/> </LinearLayout> <Button android:id="@+id/rst_next_step" android:layout_width="match_parent" android:layout_height="40dp" android:layout_margin="30dp" android:text="@string/rst_next_step" android:textSize="15sp" android:textColor="@color/white" android:background="@drawable/shape_btn"/> </LinearLayout>
這里布局有個(gè)問(wèn)題,因?yàn)楂@取驗(yàn)證碼這個(gè)TextView中的字會(huì)在倒計(jì)時(shí)期間有變化,而且每次字的變化長(zhǎng)度不一樣,如果把它的layout_width設(shè)為wrap_content,那么這個(gè)TextView的長(zhǎng)度會(huì)變化,影響界面美觀,所以可以把它的長(zhǎng)度固定,然后把驗(yàn)證碼輸入框的layout_weight設(shè)為1,這樣就可以了。
3、具體使用方法
// 發(fā)送成功進(jìn)入倒計(jì)時(shí) countDownTimer = new CountDownTimerUtils(tv_send_code, 60000, 1000); countDownTimer.start();
其中60000代表要倒計(jì)時(shí)的時(shí)長(zhǎng),即60s;1000代表每次遞減1s。
以上就是具體的實(shí)現(xiàn)過(guò)程,下面附幾張效果圖!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android利用CountDownTimer實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android賬號(hào)注冊(cè)實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android中TextView實(shí)現(xiàn)部分文字可點(diǎn)擊跳轉(zhuǎn)
- Android studio點(diǎn)擊跳轉(zhuǎn)WebView詳解
- Android中TextView自動(dòng)識(shí)別url且實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)
- Android TextView中文本點(diǎn)擊文字跳轉(zhuǎn) (代碼簡(jiǎn)單)
- Android開(kāi)發(fā)歡迎頁(yè)點(diǎn)擊跳過(guò)倒計(jì)時(shí)進(jìn)入主頁(yè)
相關(guān)文章
詳解Android獲取所有依賴(lài)庫(kù)的幾種方式
本篇文章主要介紹了詳解Android獲取所有依賴(lài)庫(kù)的幾種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07基于Flutter實(shí)現(xiàn)愛(ài)心三連動(dòng)畫(huà)效果
Animation是一個(gè)抽象類(lèi),它并不參與屏幕的繪制,而是在設(shè)定的時(shí)間范圍內(nèi)對(duì)一段區(qū)間值進(jìn)行插值。本文將利用Animation制作一個(gè)愛(ài)心三連動(dòng)畫(huà)效果,感興趣的可以學(xué)習(xí)一下2022-03-03Android開(kāi)發(fā)中聽(tīng)筒無(wú)法播放音樂(lè)的解決方法
這篇文章主要介紹了Android開(kāi)發(fā)中聽(tīng)筒無(wú)法播放音樂(lè)的解決方法,涉及Android權(quán)限控制中的相關(guān)屬性設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10Flutter框架實(shí)現(xiàn)Android拖動(dòng)到垃圾桶刪除效果
這篇文章主要介紹了Flutter框架實(shí)現(xiàn)Android拖動(dòng)到垃圾桶刪除效果,Flutter框架中的Draggable部件,用于支持用戶(hù)通過(guò)手勢(shì)拖動(dòng),它是基于手勢(shì)的一種方式,可以使用戶(hù)可以在屏幕上拖動(dòng)指定的部件,下面我們來(lái)詳細(xì)了解一下2023-12-12Android使用SurfaceView實(shí)現(xiàn)飄贊動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android如何使用SurfaceView實(shí)現(xiàn)飄贊動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03如何使用Android實(shí)現(xiàn)接口實(shí)信息在留言板顯示
這篇文章主要介紹了如何使用Android接口實(shí)現(xiàn)信息的留言板顯示,需要的朋友可以參考下2015-07-07Android語(yǔ)音識(shí)別技術(shù)詳解及實(shí)例代碼
這篇文章主要介紹了Android語(yǔ)音識(shí)別技術(shù)的相關(guān)資料,并附實(shí)例代碼及實(shí)例實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-09-09Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例
今天小編就為大家分享一篇關(guān)于Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03