RxJava的几种常见基础使用,自己的学习记录
1、RxJava 延迟执行
2、计时器:
3、倒计时:以获取验证码为例
4、合并操作:如登录页面 登录按钮 是否可点击(需要满足一定条件):
注意:返回的mDisposable 别忘记在onDestory里关掉;如果一个页面有多个Disposable可用管理器CompositeDisposable统一关掉,先添加add(),然后退出时clear()关掉全部。这个CompositeDisposable可以直接写在基类统一处理。
//方法一:在页面里单个处理
private Disposable mDisposable;
@Override
protected void onDestroy() {
super.onDestroy();
if (mDisposable != null && !mDisposable.isDisposed()) {
mDisposable.dispose();
mDisposable = null;
}
}
//方法二:在基类里统一处理 ,子类使用时调用addDisposable就行了
private CompositeDisposable compositeDisposable;
protected void addDisposable(@NonNull Disposable... ds) {
if (compositeDisposable == null) compositeDisposable = new CompositeDisposable();
compositeDisposable.addAll(ds);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (compositeDisposable != null) {
compositeDisposable.clear();
}
}
1、RxJava 延迟执行
//延迟5秒执行
private void test() {
mDisposable = Observable.timer(5, TimeUnit.SECONDS).
subscribeOn(Schedulers.io()).
observeOn(AndroidSchedulers.mainThread()).
subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
/**
* 延迟后要做的事情
*/
}
});
}
2、计时器:
private void test() {
/**
* 间隔 从0开始递增
* 参数说明:延迟发送时间、间隔时间、时间单位
*/
Observable.interval(0, 1, TimeUnit.SECONDS).
subscribeOn(Schedulers.io()).
observeOn(AndroidSchedulers.mainThread()).
subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
tv.setText(aLong + "");
}
});
}
3、倒计时:以获取验证码为例
//获取验证码
private void initGetCode() {
final long time = 60; //倒计时时间
getCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getCode.setEnabled(false);
getCode.setBackgroundColor(Color.parseColor("#999999"));
/**
* 间隔范围
*
* 参数说明:long start, long count, long initialDelay, long period, TimeUnit unit
* 参数一:从几开始(发送都是递增的)
* 参数二:发送多少次
* 参数三:延迟多久发送
* 参数四:间隔多久发送一次
* 参数五:时间单位,这里传的 秒
*/
mDisposable = Observable.intervalRange(0, time + 1, 0, 1, TimeUnit.SECONDS).
subscribeOn(Schedulers.io()).
observeOn(AndroidSchedulers.mainThread()).
subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
getCode.setText(time - aLong + "");
//倒计时完成
if (aLong == time) {
getCode.setEnabled(true);
getCode.setText("获取验证码");
getCode.setBackgroundColor(Color.parseColor("#ff0011"));
}
System.out.println(aLong);
}
});
}
});
}
4、合并操作:如登录页面 登录按钮 是否可点击(需要满足一定条件):
tv_login.setEnabled(false);
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击了登录", 1).show();
}
});
不用RxJava,可以这样写:
boolean phoneIsFinsh;
boolean codeIsFinsh;
private void isLoginClick() {
boolean aBoolean = phoneIsFinsh && codeIsFinsh;
tv_login.setBackgroundColor(Color.parseColor(aBoolean ? "#ff0011" : "#999999"));
tv_login.setEnabled(aBoolean);
}
//设置登录是否可点击
private void initLoginClickBle() {
tv_login.setEnabled(false);
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击了登录", 1).show();
}
});
ed_phone.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// emitter.onNext(charSequence.length() == 11);
phoneIsFinsh = charSequence.length() == 11;
isLoginClick();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
ed_input_code.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// emitter.onNext(charSequence.length() == 4);
codeIsFinsh = charSequence.length() == 4;
isLoginClick();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
用RxJava,可以这样写:
//设置登录是否可点击
private void initLoginClickBle() {
tv_login.setEnabled(false);
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "点击了登录", 1).show();
}
});
Observable<Boolean> observable1 = Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception {
ed_phone.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
emitter.onNext(charSequence.length() == 11);
// phoneIsFinsh = charSequence.length() == 11;
// isLoginClick();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
});
Observable<Boolean> observable2 = Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception {
ed_input_code.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
emitter.onNext(charSequence.length() == 4);
// codeIsFinsh = charSequence.length() == 4;
// isLoginClick();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
});
//如果是有3个及以上的条件,可以用 Function3/4/5...9
Observable.combineLatest(observable1, observable2, new BiFunction<Boolean, Boolean, Boolean>() {
@Override
public Boolean apply(Boolean aBoolean, Boolean aBoolean2) throws Exception {
return aBoolean && aBoolean2;
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
tv_login.setBackgroundColor(Color.parseColor(aBoolean ? "#ff0011" : "#999999"));
tv_login.setEnabled(aBoolean);
}
});
}
网友评论