RxJava的幾種常見(jiàn)基礎(chǔ)使用,自己的學(xué)習(xí)記錄
1、RxJava 延遲執(zhí)行
2、計(jì)時(shí)器:
3、倒計(jì)時(shí):以獲取驗(yàn)證碼為例
4、合并操作:如登錄頁(yè)面 登錄按鈕 是否可點(diǎn)擊(需要滿足一定條件):
注意:返回的mDisposable 別忘記在onDestory里關(guān)掉;如果一個(gè)頁(yè)面有多個(gè)Disposable可用管理器CompositeDisposable統(tǒng)一關(guān)掉,先添加add(),然后退出時(shí)clear()關(guān)掉全部。這個(gè)CompositeDisposable可以直接寫(xiě)在基類統(tǒng)一處理。
//方法一:在頁(yè)面里單個(gè)處理
private Disposable mDisposable;
@Override
protected void onDestroy() {
super.onDestroy();
if (mDisposable != null && !mDisposable.isDisposed()) {
mDisposable.dispose();
mDisposable = null;
}
}
//方法二:在基類里統(tǒng)一處理 ,子類使用時(shí)調(diào)用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 延遲執(zhí)行
//延遲5秒執(zhí)行
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、計(jì)時(shí)器:
private void test() {
/**
* 間隔 從0開(kāi)始遞增
* 參數(shù)說(shuō)明:延遲發(fā)送時(shí)間、間隔時(shí)間、時(shí)間單位
*/
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、倒計(jì)時(shí):以獲取驗(yàn)證碼為例
//獲取驗(yàn)證碼
private void initGetCode() {
final long time = 60; //倒計(jì)時(shí)時(shí)間
getCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getCode.setEnabled(false);
getCode.setBackgroundColor(Color.parseColor("#999999"));
/**
* 間隔范圍
*
* 參數(shù)說(shuō)明:long start, long count, long initialDelay, long period, TimeUnit unit
* 參數(shù)一:從幾開(kāi)始(發(fā)送都是遞增的)
* 參數(shù)二:發(fā)送多少次
* 參數(shù)三:延遲多久發(fā)送
* 參數(shù)四:間隔多久發(fā)送一次
* 參數(shù)五:時(shí)間單位,這里傳的 秒
*/
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 + "");
//倒計(jì)時(shí)完成
if (aLong == time) {
getCode.setEnabled(true);
getCode.setText("獲取驗(yàn)證碼");
getCode.setBackgroundColor(Color.parseColor("#ff0011"));
}
System.out.println(aLong);
}
});
}
});
}
4、合并操作:如登錄頁(yè)面 登錄按鈕 是否可點(diǎn)擊(需要滿足一定條件):
tv_login.setEnabled(false);
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "點(diǎn)擊了登錄", 1).show();
}
});
不用RxJava,可以這樣寫(xiě):
boolean phoneIsFinsh;
boolean codeIsFinsh;
private void isLoginClick() {
boolean aBoolean = phoneIsFinsh && codeIsFinsh;
tv_login.setBackgroundColor(Color.parseColor(aBoolean ? "#ff0011" : "#999999"));
tv_login.setEnabled(aBoolean);
}
//設(shè)置登錄是否可點(diǎn)擊
private void initLoginClickBle() {
tv_login.setEnabled(false);
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "點(diǎn)擊了登錄", 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,可以這樣寫(xiě):
//設(shè)置登錄是否可點(diǎn)擊
private void initLoginClickBle() {
tv_login.setEnabled(false);
tv_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "點(diǎn)擊了登錄", 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個(gè)及以上的條件,可以用 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);
}
});
}