欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時(shí)功能的示例代碼

 更新時(shí)間:2018年03月08日 10:21:14   作者:tnnowu  
本篇文章主要介紹了Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時(shí)功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

示例圖:


示例代碼:

導(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'

布局文件(很簡(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();
    }
  }
}

說明:① 在doOnNext里面做倒計(jì)時(shí)UI更改,在doOnComplete里面做倒計(jì)時(shí)完成之后的操作,如彈Toast或者跳轉(zhuǎn)等;② 我們調(diào)用重復(fù)執(zhí)行的方法,所以要在onDestroy方法中取消訂閱。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論