Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時(shí)功能的示例代碼
倒計(jì)時(shí)功能被廣泛運(yùn)用在 App 啟動(dòng)頁(yè)、短信驗(yàn)證碼倒計(jì)時(shí)等,通常做法是起一個(gè)Handler ,在子線程里完成倒計(jì)時(shí),如今這一做法有了替代品 —— RxJava ,RxJava是被行內(nèi)一致認(rèn)可的第三方開(kāi)源庫(kù),我們可以使用RxJava實(shí)現(xiàn)倒計(jì)時(shí)功能。
示例圖:

示例代碼:
導(dǎo)入必要的庫(kù)文件(Android支持庫(kù)和Reactivex系列支持庫(kù))
implementation 'com.android.support:appcompat-v7:27.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.1.10'
布局文件(很簡(jiǎn)單,只有一個(gè)TextView)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.haocent.android.countdown.MainActivity">
<TextView
android:id="@+id/tv_count_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Hello World!" />
</android.support.constraint.ConstraintLayout>
實(shí)現(xiàn)倒計(jì)時(shí)功能(代碼清晰明了,也打出了相應(yīng)的Log)
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Disposable mDisposable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final TextView tvCountDown = findViewById(R.id.tv_count_down);
// 倒計(jì)時(shí) 10s
mDisposable = Flowable.intervalRange(0, 11, 0, 1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
Log.d(TAG, "倒計(jì)時(shí)");
tvCountDown.setText("倒計(jì)時(shí) " + String.valueOf(10 - aLong) + " 秒");
}
})
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "倒計(jì)時(shí)完畢");
Toast.makeText(MainActivity.this, "倒計(jì)時(shí)完畢", Toast.LENGTH_SHORT).show();
}
})
.subscribe();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mDisposable != null) {
mDisposable.dispose();
}
}
}
說(shuō)明:① 在doOnNext里面做倒計(jì)時(shí)UI更改,在doOnComplete里面做倒計(jì)時(shí)完成之后的操作,如彈Toast或者跳轉(zhuǎn)等;② 我們調(diào)用重復(fù)執(zhí)行的方法,所以要在onDestroy方法中取消訂閱。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法分析
這篇文章主要介紹了Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法,結(jié)合具體實(shí)例形式分析了時(shí)間控件DatePicker和TimePicke布局與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02
android實(shí)現(xiàn)緩存圖片等數(shù)據(jù)
本文給大家分享的是Android采用LinkedHashMap自帶的LRU 算法緩存數(shù)據(jù)的方法和示例,有需要的小伙伴可以參考下。2015-07-07
android 左右滑動(dòng)+索引圖標(biāo)實(shí)現(xiàn)方法與代碼
使用Gallery和ImageView實(shí)現(xiàn)android左右滑動(dòng)+索引圖標(biāo)效果,接下來(lái)詳細(xì)介紹,有需要的朋友可以參考下2012-12-12
android app判斷是否有系統(tǒng)簽名步驟詳解
這篇文章主要為大家介紹了android app判斷是否有系統(tǒng)簽名步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Android利用ZXing掃描二維碼的實(shí)例代碼解析
這篇文章主要介紹了Android利用ZXing掃描二維碼的實(shí)例解析,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的朋友可以參考下2016-12-12
Kotlin?Flow數(shù)據(jù)流的3種使用場(chǎng)景詳解
這篇文章主要為大家詳細(xì)介紹了Kotlin中Flow數(shù)據(jù)流的幾種使用場(chǎng)景,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-04-04
Android WebView實(shí)現(xiàn)網(wǎng)頁(yè)滾動(dòng)截圖
這篇文章主要為大家詳細(xì)介紹了Android WebView實(shí)現(xiàn)網(wǎng)頁(yè)滾動(dòng)截圖,對(duì)整個(gè)網(wǎng)頁(yè)進(jìn)行截屏,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
一文教你如何使用Databinding寫一個(gè)關(guān)注功能
這篇文章主要介紹了一文教你如何使用Databinding寫一個(gè)關(guān)注功能,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

