這篇“Android怎么實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Android怎么實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能”文章吧。
場(chǎng)景:注冊(cè)賬號(hào)頁面時(shí),我們點(diǎn)擊按鈕發(fā)送驗(yàn)證碼,在等待驗(yàn)證碼時(shí),界面會(huì)有倒計(jì)時(shí)提示,這此期間按鈕不可點(diǎn)擊。當(dāng)?shù)褂?jì)時(shí)結(jié)束時(shí),按鈕恢復(fù)。
實(shí)現(xiàn)與功能都不難,這次用 RxBinding,RxJava2 的方法去實(shí)現(xiàn)。并實(shí)現(xiàn)了手動(dòng)、自動(dòng)停止倒計(jì)時(shí),防止多次點(diǎn)擊。
功能動(dòng)態(tài)圖
要使用 RxBinding、RxJava2 先添加 Gradle 配置:
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0.1' compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0' compile 'com.jakewharton.rxbinding2:rxbinding-support-v4:2.0.0' compile 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.0.0'
首先通過 RxView.clicks() 綁定并轉(zhuǎn)換成一個(gè)倒計(jì)時(shí)的 Observable 觀察者對(duì)象。
Observable<Long> mObservableCountTime = RxView.clicks(mBtnSendMsm) //防止重復(fù)點(diǎn)擊 .throttleFirst(MAX_COUNT_TIME, TimeUnit.SECONDS) //將點(diǎn)擊事件轉(zhuǎn)換成倒計(jì)時(shí)事件 .flatMap(new Function<Object, ObservableSource<Long>>() { @Override public ObservableSource<Long> apply(Object o) throws Exception { //更新發(fā)送按鈕的狀態(tài)并初始化顯現(xiàn)倒計(jì)時(shí)文字 RxView.enabled(mBtnSendMsm).accept(false); RxTextView.text(mBtnSendMsm).accept("剩余 " + MAX_COUNT_TIME + " 秒"); //在實(shí)際操作中可以在此發(fā)送獲取網(wǎng)絡(luò)的請(qǐng)求 //返回 N 秒內(nèi)的倒計(jì)時(shí)觀察者對(duì)象。 return Observable.interval(1, TimeUnit.SECONDS, Schedulers.io()).take(MAX_COUNT_TIME); } }) //將遞增數(shù)字替換成遞減的倒計(jì)時(shí)數(shù)字 .map(new Function<Long, Long>() { @Override public Long apply(Long aLong) throws Exception { return MAX_COUNT_TIME - (aLong + 1); } }) .observeOn(AndroidSchedulers.mainThread());//切換到 Android 的主線程。
設(shè)置作為倒計(jì)時(shí)提示的 Consumer 被觀察者對(duì)象。
Consumer<Long> mConsumerCountTime = new Consumer<Long>() { @Override public void accept(Long aLong) throws Exception { //顯示剩余時(shí)長(zhǎng)。當(dāng)?shù)褂?jì)時(shí)為 0 時(shí),還原 btn 按鈕. if (aLong == 0) { RxView.enabled(mBtnSendMsm).accept(true); RxTextView.text(mBtnSendMsm).accept("發(fā)送驗(yàn)證碼"); } else { RxTextView.text(mBtnSendMsm).accept("剩余 " + aLong + " 秒"); } } };
訂閱點(diǎn)擊事件:
//訂閱點(diǎn)擊事件 Disposable mDisposable = mObservableCountTime.subscribe(mConsumerCountTime);
停止倒計(jì)時(shí),但依然可以再次點(diǎn)擊。
//重置驗(yàn)證碼按鈕。 RxView.clicks(mBtnClean).subscribe(new Consumer<Object>() { @Override public void accept(Object o) throws Exception { if (mDisposable != null && !mDisposable.isDisposed()) { //停止倒計(jì)時(shí) mDisposable.dispose(); //重新訂閱 mDisposable = mObservableCountTime.subscribe(mConsumerCountTime); //按鈕可點(diǎn)擊 RxView.enabled(mBtnSendMsm).accept(true); RxTextView.text(mBtnSendMsm).accept("發(fā)送驗(yàn)證碼"); } } });
退出當(dāng)前頁面時(shí),銷毀清空數(shù)據(jù)。
@Override protected void onDestroy() { super.onDestroy(); if (mDisposable != null) { mDisposable.dispose(); } }
以上就是關(guān)于“Android怎么實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:Android怎么實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://jinyejixie.com/article8/disgip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、商城網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、建站公司、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容