2018-06-29 11:23:59 1645瀏覽
一般我們?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)行線程切換 *@paramview */ publicvoidgetSureCode(Viewview){ Observable.create(newObservableOnSubscribe<Integer>(){ @Override publicvoidsubscribe(ObservableEmitter<Integer>emitter)throwsException{ inti=10; while(i>=0){ try{ Thread.sleep(1000); emitter.onNext(i); }catch(InterruptedExceptione){ e.printStackTrace(); } i--; } emitter.onComplete(); } }).subscribeOn(Schedulers.io())//此方法為上面發(fā)出事件設(shè)置線程為IO線程 .observeOn(AndroidSchedulers.mainThread())//為消耗事件設(shè)置線程為UI線程 .subscribe(newConsumer<Integer>(){ @Override publicvoidaccept(Integerinteger)throwsException{ 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){ Stringcontent=integer+"秒后可重新發(fā)送"; SpannableStringspan=newSpannableString(content); intindex=content.indexOf("后"); span.setSpan(newForegroundColorSpan(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)行控件的綁定:
<layoutxmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayoutxmlns: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>
<?xmlversion="1.0"encoding="utf-8"?> <shapexmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--填充顏色--> <solidandroid:color="@color/colorLineItem"></solid> <!--線的寬度,顏色灰色--> <strokeandroid:width="1dp"android:color="@color/colorLineItem"></stroke> <!--矩形的圓角半徑--> <cornersandroid:radius="5dp"/> </shape>
rectangle_gray_bg.xml
<?xmlversion="1.0"encoding="utf-8"?> <shapexmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--填充顏色--> <solidandroid:color="@color/colorTheme"></solid> <!--線的寬度,顏色灰色--> <strokeandroid:width="1dp"android:color="@color/colorTheme"></stroke> <!--矩形的圓角半徑--> <cornersandroid:radius="5dp"/> </shape>
<colorname="colorLineItem">#FFDDDDDD</color> <colorname="colorTheme">#f64a33</color>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,讓大家的知識(shí)可以更進(jìn)一步,對(duì)Android有深入的了解,如果你想學(xué)習(xí)更多的Android知識(shí),對(duì)Android有全面的了解,請(qǐng)關(guān)注扣丁學(xué)堂微信公眾號(hào),或登錄扣丁學(xué)堂官網(wǎng)了解更多,還有大量Android在線視頻教程等著你來(lái)觀看?。?!
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Android開(kāi)發(fā)技術(shù)”的相關(guān)資訊>>