android使用Rxjava實(shí)現(xiàn)倒計(jì)時(shí)功能
一般我們?cè)陂_發(fā)時(shí),常會(huì)遇到使用倒計(jì)時(shí)的場景,以前一般會(huì)使用thread+handler來實(shí)現(xiàn),而強(qiáng)大的Rxjava橫空出世后,使這一切變得簡單了。我們可以在子線程中直接使用發(fā)射器每融1S發(fā)出一個(gè)時(shí)間,在主線程中接收更新ui,在等倒計(jì)時(shí)結(jié)束恢復(fù)界面,下面給出在用戶注冊(cè)時(shí)獲取驗(yàn)證碼的,倒計(jì)時(shí)使用的代碼demo。具體調(diào)用方法如下:
/**
* 點(diǎn)擊獲取驗(yàn)證碼,10S倒計(jì)時(shí),利用Rxjava進(jìn)行線程切換
* @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));
}
}
});
}
下面的是布局文件,布局只有一個(gè)TextView控件,這里采用了dataBinding進(jìn)行控件的綁定:
<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>
這里定義了兩個(gè)drawable用來對(duì)倒計(jì)時(shí)背景的更換,倒計(jì)時(shí)時(shí)不允許對(duì)控件進(jìn)行點(diǎn)擊:
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>
兩個(gè)顏色值:
<color name="colorLineItem">#FFDDDDDD</color> <color name="colorTheme">#f64a33</color>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android上實(shí)現(xiàn)easyconfig(airkiss)方法
本篇文章主要給大家講解了在Android系統(tǒng)上實(shí)現(xiàn)easyconfig(airkiss)的方法,有這方面需要的朋友參考學(xué)習(xí)下吧。2018-01-01
Android 通過TCP協(xié)議上傳指定目錄文件的方法
這篇文章主要介紹了Android 通過TCP協(xié)議上傳指定目錄文件的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
android中Webview實(shí)現(xiàn)截屏三種方式小結(jié)
本篇文章主要介紹了android Webview實(shí)現(xiàn)截屏,主要詳解了3種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(一)
這篇文章主要介紹了Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(一) 的相關(guān)資料,需要的朋友可以參考下2016-01-01

