一般我们在开发时,常会遇到使用倒计时的场景,以前一般会使用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