I have an edittext where user enters some amount, I have a continue button which is disabled by default and is only enabled if the amount is valid and within some range,Everything works fine until I try to format the amount as $2000 or $10,000. Here is how I am using WidgetObservable on an edit text.
Observable<Boolean> amountValidObervable = WidgetObservable.text(amount, false)
.debounce(500,TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.map(new Func1<OnTextChangeEvent, Boolean>() {
@Override
public Boolean call(OnTextChangeEvent event) {
CharSequence amount = event.text();
return isValid(amount);
}
});
formChangeSubscription = amountValidObservable.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean isValid) {
amountLayout.setErrorEnabled(isValid ? false : true);
amountLayout.setError(isValid ? null : "Please enter amount within the range");
}
});
amountValidObservable.subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
log.debug("formChangeObservable completed");
}
@Override
public void onError(Throwable e) {
log.debug("formChangeObservable error!");
}
@Override
public void onNext(Boolean formValid) {
nextBtn.setBackgroundResource(R.drawable.btn);
nextBtn.setEnabled(formValid);
}
});
I want to format the text of the edit text using something like
DecimalFormat decimalFormat = new DecimalFormat("$#,###");
How can I format text using this formatter with RxJava as the user is typing. Please Note, Due to some dependency I cannot use RxBindings yet.
The problem that you have 2 subscribers to 1 Observable, so would be good to refCunt it! Just add .replay(1).refCount()
at the end of amountValidObervable
So it will be:
WidgetObservable.text(amount, false)
.debounce(500,TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.map(new Func1<OnTextChangeEvent, Boolean>() {
@Override
public Boolean call(OnTextChangeEvent event) {
CharSequence amount = event.text();
return isValid(amount);
}
})
.replay(1)
.refCount();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.