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

示例代碼:
導(dǎo)入必要的庫文件(Android支持庫和Reactivex系列支持庫)
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'
布局文件(很簡單,只有一個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>
實現(xiàn)倒計時功能(代碼清晰明了,也打出了相應(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);
// 倒計時 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, "倒計時");
tvCountDown.setText("倒計時 " + String.valueOf(10 - aLong) + " 秒");
}
})
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "倒計時完畢");
Toast.makeText(MainActivity.this, "倒計時完畢", Toast.LENGTH_SHORT).show();
}
})
.subscribe();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mDisposable != null) {
mDisposable.dispose();
}
}
}
說明:① 在doOnNext里面做倒計時UI更改,在doOnComplete里面做倒計時完成之后的操作,如彈Toast或者跳轉(zhuǎn)等;② 我們調(diào)用重復(fù)執(zhí)行的方法,所以要在onDestroy方法中取消訂閱。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程之DatePicker和TimePicke簡單時間監(jiān)聽用法分析
這篇文章主要介紹了Android編程之DatePicker和TimePicke簡單時間監(jiān)聽用法,結(jié)合具體實例形式分析了時間控件DatePicker和TimePicke布局與具體功能實現(xiàn)技巧,需要的朋友可以參考下2017-02-02
android實現(xiàn)緩存圖片等數(shù)據(jù)
本文給大家分享的是Android采用LinkedHashMap自帶的LRU 算法緩存數(shù)據(jù)的方法和示例,有需要的小伙伴可以參考下。2015-07-07
android 左右滑動+索引圖標(biāo)實現(xiàn)方法與代碼
使用Gallery和ImageView實現(xiàn)android左右滑動+索引圖標(biāo)效果,接下來詳細(xì)介紹,有需要的朋友可以參考下2012-12-12
android app判斷是否有系統(tǒng)簽名步驟詳解
這篇文章主要為大家介紹了android app判斷是否有系統(tǒng)簽名步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Kotlin?Flow數(shù)據(jù)流的3種使用場景詳解
這篇文章主要為大家詳細(xì)介紹了Kotlin中Flow數(shù)據(jù)流的幾種使用場景,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以參考一下2023-04-04
Android WebView實現(xiàn)網(wǎng)頁滾動截圖
這篇文章主要為大家詳細(xì)介紹了Android WebView實現(xiàn)網(wǎng)頁滾動截圖,對整個網(wǎng)頁進(jìn)行截屏,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
一文教你如何使用Databinding寫一個關(guān)注功能
這篇文章主要介紹了一文教你如何使用Databinding寫一個關(guān)注功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09

