android使用Rxjava實(shí)現(xiàn)倒計(jì)時(shí)功能
一般我們?cè)陂_(kāi)發(fā)時(shí),常會(huì)遇到使用倒計(jì)時(shí)的場(chǎng)景,以前一般會(huì)使用thread+handler來(lái)實(shí)現(xiàn),而強(qiáng)大的Rxjava橫空出世后,使這一切變得簡(jiǎn)單了。我們可以在子線程中直接使用發(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用來(lái)對(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Rxjava3 使用場(chǎng)景詳解
- Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時(shí)功能的示例代碼
- Android RxJava創(chuàng)建操作符Timer的方法
- Android RxJava創(chuàng)建操作符Interval
- Android中Rxjava實(shí)現(xiàn)三級(jí)緩存的兩種方式
- Android Retrofit和Rxjava的網(wǎng)絡(luò)請(qǐng)求
- android使用RxJava實(shí)現(xiàn)預(yù)加載
- Android RxJava異步數(shù)據(jù)處理庫(kù)使用詳解
相關(guān)文章
Android getViewById和getLayoutInflater().inflate()的詳解及比較
這篇文章主要介紹了Android getViewById和getLayoutInflater().inflate()的詳解及比較的相關(guān)資料,這里對(duì)這兩種方法進(jìn)行了詳細(xì)的對(duì)比,對(duì)于開(kāi)始學(xué)習(xí)Android的朋友使用這兩種方法是個(gè)很好的資料,需要的朋友可以參考下2016-11-11
android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例
本篇文章主要介紹了android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Android Cocos Creator游戲開(kāi)發(fā)平臺(tái)打包優(yōu)化實(shí)現(xiàn)方案
Cocos Creator是一款輕量、高效、免費(fèi)開(kāi)源的跨平臺(tái)游戲引擎,同時(shí)也是實(shí)時(shí)3D內(nèi)容創(chuàng)作平臺(tái),不僅支持2D、3D的游戲開(kāi)發(fā),同時(shí)在HMI、IoT、XR、虛擬人偶等領(lǐng)域,均可提供一套完善的行業(yè)解決方案2022-11-11
Android實(shí)現(xiàn)圖片自動(dòng)切換功能(實(shí)例代碼詳解)
這篇文章主要介紹了Android實(shí)現(xiàn)圖片自動(dòng)切換功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Android開(kāi)發(fā)之MediaPlayer基本使用方法詳解
這篇文章主要介紹了Android開(kāi)發(fā)之MediaPlayer基本使用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了MediaPlayer中的常用函數(shù)與基本使用技巧,需要的朋友可以參考下2017-05-05
Android基于Aidl的跨進(jìn)程間雙向通信管理中心
這篇文章主要為大家詳細(xì)介紹了Android基于Aidl的跨進(jìn)程間雙向通信管理中心,類似于聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android 中ViewPager重排序與更新實(shí)例詳解
這篇文章主要介紹了Android 中ViewPager重排序與更新實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn)
這篇文章主要介紹了flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

