一般我們在開發時,常會遇到使用倒計時的場景,以前一般會使用thread+handler來實現,而強大的Rxjava橫空出世后,使這一切變得簡單了。我們可以在子線程中直接使用發射器每融1S發出一個時間,在主線程中接收更新ui,在等倒計時結束恢復界面,下面給出在用戶注冊時獲取驗證碼的,倒計時使用的代碼demo。具體調用方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/** * 點擊獲取驗證碼,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()) // 此方法為上面發出事件設置線程為IO線程 .observeOn(AndroidSchedulers.mainThread()) // 為消耗事件設置線程為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 + "秒后可重新發送" ; SpannableString span = new SpannableString(content); int index = content.indexOf( "后" ); span.setSpan( new ForegroundColorSpan(getResources().getColor(R.color.colorTheme)), 0 , index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //設置前景色為紅色 bindingView.countDownTv.setText(span); } else { bindingView.countDownTv.setText(getString(R.string.get_check_code)); } } }); } |
下面的是布局文件,布局只有一個TextView控件,這里采用了dataBinding進行控件的綁定:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
< 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文件
1
2
3
4
5
6
7
8
9
10
|
<? 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
1
2
3
4
5
6
7
8
9
10
|
<? 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 > |
兩個顏色值:
1
2
|
< color name = "colorLineItem" >#FFDDDDDD</ color > < color name = "colorTheme" >#f64a33</ color > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/smileiam/article/details/68927406