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

android使用Rxjava實現(xiàn)倒計時功能

 更新時間:2018年06月28日 14:48:05   作者:瀟瀟鳳兒  
這篇文章主要為大家詳細介紹了android使用Rxjava實現(xiàn)倒計時功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一般我們在開發(fā)時,常會遇到使用倒計時的場景,以前一般會使用thread+handler來實現(xiàn),而強大的Rxjava橫空出世后,使這一切變得簡單了。我們可以在子線程中直接使用發(fā)射器每融1S發(fā)出一個時間,在主線程中接收更新ui,在等倒計時結(jié)束恢復(fù)界面,下面給出在用戶注冊時獲取驗證碼的,倒計時使用的代碼demo。具體調(diào)用方法如下:

 /**
 * 點擊獲取驗證碼,10S倒計時,利用Rxjava進行線程切換
 * @param view
 */
 public void getSureCode(View view) {
 Observable.create(new ObservableOnSubscribe<Integer>() {
  @Override
  public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
  int i = 10;
  while (i >= 0) {
   try {
   Thread.sleep(1000);
   emitter.onNext(i);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
   i--;
  }
  emitter.onComplete();
  }
 }).subscribeOn(Schedulers.io())// 此方法為上面發(fā)出事件設(shè)置線程為IO線程
  .observeOn(AndroidSchedulers.mainThread())// 為消耗事件設(shè)置線程為UI線程
  .subscribe(new Consumer<Integer>() {
   @Override
   public void accept(Integer integer) throws Exception {
   bindingView.countDownTv.setClickable(integer > 0 ? false : true);
   bindingView.countDownTv.setBackground(integer > 0 ? getResources().getDrawable(R.drawable.rectangle_gray_bg) : getResources().getDrawable(R.drawable.rectangle_red_bg));
   if(integer > 0) {
    String content = integer + "秒后可重新發(fā)送";
    SpannableString span = new SpannableString(content);
    int index = content.indexOf("后");
    span.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorTheme)), 0, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //設(shè)置前景色為紅色
    bindingView.countDownTv.setText(span);
   } else {
    bindingView.countDownTv.setText(getString(R.string.get_check_code));
   }
   }
  });
 }

下面的是布局文件,布局只有一個TextView控件,這里采用了dataBinding進行控件的綁定:

<layout xmlns:android="http://schemas.android.com/apk/res/android">

 <LinearLayout xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.smilexie.countdownwithrxjava.MainActivity">

 <TextView
  android:id="@+id/count_down_tv"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_margin="10dp"
  android:gravity="center"
  android:layout_gravity="center_vertical"
  android:padding="8dp"
  android:background="@drawable/rectangle_red_bg"
  android:text="@string/get_check_code"
  android:textSize="14sp"
  android:textColor="@color/white"
  android:onClick="getSureCode"/>

 </LinearLayout>
</layout>


這里定義了兩個drawable用來對倒計時背景的更換,倒計時時不允許對控件進行點擊:
rectangle_gray_bg.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <!-- 填充顏色 -->
 <solid android:color="@color/colorLineItem"></solid>
 <!-- 線的寬度,顏色灰色 -->
 <stroke android:width="1dp" android:color="@color/colorLineItem"></stroke>
 <!-- 矩形的圓角半徑 -->
 <corners android:radius="5dp" />
</shape>

rectangle_gray_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <!-- 填充顏色 -->
 <solid android:color="@color/colorTheme"></solid>
 <!-- 線的寬度,顏色灰色 -->
 <stroke android:width="1dp" android:color="@color/colorTheme"></stroke>
 <!-- 矩形的圓角半徑 -->
 <corners android:radius="5dp" />
</shape>

兩個顏色值:

<color name="colorLineItem">#FFDDDDDD</color>
<color name="colorTheme">#f64a33</color>

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

相關(guān)文章

  • Android上實現(xiàn)easyconfig(airkiss)方法

    Android上實現(xiàn)easyconfig(airkiss)方法

    本篇文章主要給大家講解了在Android系統(tǒng)上實現(xiàn)easyconfig(airkiss)的方法,有這方面需要的朋友參考學(xué)習(xí)下吧。
    2018-01-01
  • 詳解Android跨進程通信之AIDL

    詳解Android跨進程通信之AIDL

    這篇文章主要介紹了詳解Android跨進程通信之AIDL,想了解跨進程的同學(xué)可以參考下
    2021-04-04
  • Android 通過TCP協(xié)議上傳指定目錄文件的方法

    Android 通過TCP協(xié)議上傳指定目錄文件的方法

    這篇文章主要介紹了Android 通過TCP協(xié)議上傳指定目錄文件的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • android中Webview實現(xiàn)截屏三種方式小結(jié)

    android中Webview實現(xiàn)截屏三種方式小結(jié)

    本篇文章主要介紹了android Webview實現(xiàn)截屏,主要詳解了3種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android APP存活檢測方式

    Android APP存活檢測方式

    這篇文章主要介紹了Android APP存活檢測方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android實現(xiàn)全局右滑返回

    Android實現(xiàn)全局右滑返回

    這篇文章主要為大家詳細介紹了Android實現(xiàn)全局右滑返回,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Android如何獲取APP啟動時間

    Android如何獲取APP啟動時間

    大家好,本篇文章主要講的是Android如何獲取APP啟動時間,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Android開發(fā)筆記之Android中數(shù)據(jù)的存儲方式(一)

    Android開發(fā)筆記之Android中數(shù)據(jù)的存儲方式(一)

    這篇文章主要介紹了Android開發(fā)筆記之Android中數(shù)據(jù)的存儲方式(一) 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • android自定義左側(cè)滑出菜單效果

    android自定義左側(cè)滑出菜單效果

    這篇文章主要為大家詳細介紹了android自定義左側(cè)滑出菜單效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android 單線程模型詳解及實例

    Android 單線程模型詳解及實例

    這篇文章主要介紹了Android 單線程模型詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論