RxBinding系列之RxCompoundButton(三)
来源:互联网 发布:windows部署添加驱动 编辑:程序博客网 时间:2024/05/06 22:28
前言
本篇将通过一个实际场景来学习RxBinding中的RxCompoundButton,J大神将Android中CompoundButton的一些事件及动作加以RxJava的观察者模式并封装了起来就形成了RxCompoundButton,使用起来也很简单。
场景:注册时需用户点击同意用户协议选中框才可点击注册按钮。
布局
布局中更需要一个注册Button和一个用户协议选中框CheckBox。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="15dp" tools:context="com.leiholmes.rxbindingdemo.ui.RxCompoundButtonActivity"> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorGray" android:text="注册" /> <CheckBox android:id="@+id/cb_contract" android:layout_width="wrap_content" android:layout_marginTop="10dp" android:layout_height="wrap_content" android:text="用户协议" /></LinearLayout>
Activity
View注入
使用ButterKnife获取Button与CheckBox实例。
@BindView(R.id.btn_login)Button btnLogin;@BindView(R.id.cb_contract)CheckBox cbContract;
checkedChanges选中状态改变事件
RxCompoundButton.checkedChanges(CompoundButton view)
,内部封装了OnCheckedChangeListener选中状态改变监听。
//默认注册按钮不可点击btnLogin.setEnabled(false);addDisposable(RxCompoundButton.checkedChanges(cbContract) .subscribe(aBoolean -> { RxView.enabled(btnLogin).accept(aBoolean); btnLogin.setBackgroundResource(aBoolean ? R.color.colorPrimary : R.color.colorGray); RxTextView.color(btnLogin).accept(aBoolean ? Color.parseColor("#ffffff") : Color.parseColor("#000000")); }));addDisposable(RxView.clicks(btnLogin) //防抖2s .throttleFirst(2, TimeUnit.SECONDS) .subscribe(o -> Toast.makeText(RxCompoundButtonActivity.this, "注册成功", Toast.LENGTH_SHORT).show()));
默认注册按钮不可点击,当CheckBox被选中后则可点击注册,并修改注册按钮的样式。
View操作
RxCompoundButton中也封装了CompoundButton中例如setchecked()
、toggle()
等常用的操作,使用方式如下:
addDisposable(RxView.clicks(btnLogin) .subscribe(o -> { RxCompoundButton.checked(cbContract).accept(true); RxCompoundButton.toggle(cbContract).accept(null); }));
运行效果
最后看一下运行效果Gif。
![运行效果](http://img.blog.csdn.net/20171113114324208?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGVpSG9sbWVz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
本文疑问
addDisposable()方法什么鬼?
飞机到本系列第一篇有讲解:
RxBinding系列之RxView(一)
Lambda表达式什么鬼?
飞机到我写的Lambda表达式教程:
Lambda表达式基本语法与应用
总结
通过实际场景来学习新知识掌握起来肯定比死啃理论快,建议码友们都上手试试。
进阶中的码猿一枚,写的不对的地方欢迎大神们留言指正,有什么疑惑或者建议也可以在我Github上RxBindingDemo项目Issues中提出,我会及时回复。
附上Demo的地址:
RxBindingDemo
阅读全文
0 0
- RxBinding系列之RxCompoundButton(三)
- RxBinding系列之RxView(一)
- RxBinding系列之RxTextView(二)
- RxBinding系列之RxAdapterView(四)
- Android 开发 RX系列 之 RxBinding
- RxBinding详解之clicks事件源码解析
- SCCM2012系列之三
- 【RxJava Demo分析】(三)buffer操作符、RxBinding库
- 关于Midea系列之三
- 关于打印系列之三
- 关于产品系列之三
- 《解剖PetShop》系列之三
- 《解剖PetShop》系列之三
- sybase 珍藏系列之三
- 单片机系列(三)之修改
- 《解剖PetShop》系列之三
- JAVA ProgressBar系列之三
- 《解剖PetShop》系列之三
- 【Android源码系列】BroadcastReceiver启动源码解析
- JS IOS/iPhone的Safari不兼容Javascript中的Date()问题
- 数据库的读写分离
- 命令模式-《Head First 设计模式》第六章
- SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting
- RxBinding系列之RxCompoundButton(三)
- android 焦点分发源码解析
- HDU 2602: Bone Collector
- Linux音频设备两种框架OSS和ALSA驱动
- 【链接】12个经典大气的HTML5/CSS3应用动画
- MD5加密+加盐
- Java集合类概述《三》
- Flex布局
- php 导出excel 进度条显示