Android攻略

Rxjava之RxTextView经典详解

2018-07-20  本文已影响490人  NoValue

大家都知道Rxjava神通广大,无所不能,并有效避免Call Hell(回调地狱)模式,使大量的监听事件clickListener变换成流式编程,提高代码阅读性,提升代码维护成本。今天我通过例子详解Rxjava中的RxTextview详解,让你一举搞懂Rxjava用法,语法以及操作实例(明白 简单掌握)。

实际用户场景举例

根据用户输入的价格(价格范围0.-1)、输入的数量(10-无限)计算出总预计收入,如果写错了(举例价格范围错误、数量不对)卖出按钮无效(disable).
如果用传统的edittext.setOnChangeListner要加两个,动态计算较为难以理解。用Rxjava 则合并两个动态计算的Observable,最后动态算出结果,代码如下。

  Observable<CharSequence> priceObservable = RxTextView.textChanges(priceEdit.getEditText());
        Observable<CharSequence> countObservable = RxTextView.textChanges(countEdit.getEditText());
        sellBtnSubscription = Observable.combineLatest(priceObservable, countObservable,
                (priceSequence, countSequence) -> {
                    if (priceSequence.toString().startsWith(".")) {
                        priceSequence = "0" + priceSequence;
                        priceEdit.setText("0.");
                        priceEdit.getEditText().setSelection(priceEdit.getText().length());
                    }
                    double inputPrice = StringUtils.isEmpty(priceSequence.toString()) ? 0 : Double.parseDouble(priceSequence.toString());
                    int inputCount = StringUtils.isEmpty(countSequence.toString()) ? 0 : Integer.parseInt(countSequence.toString());
                    return inputPrice > 0 && inputCount > 0;
                }).subscribe(new Observer<Boolean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(Boolean isSellBtnEnable) {
                sellBtn.setEnabled(isSellBtnEnable);
                String handFeetStr = "(含手续费)";
                if (isSellBtnEnable) {
                    double predictedIncome = Double.parseDouble(priceEdit.getText().toString()) * Integer.parseInt(countEdit.getText());
                    predictedIncomeTv.setText(FormatUtil.formatMoney(predictedIncome) + "元");        //卖出联豆的预计收入(没有去除手续费的预估收入)
                    //( 含手续费 )
                    if (predictedIncome == 0) {
                        predictedIncomeTv.setText(FormatUtil.formatMoney(predictedIncome) + "元");        //卖出联豆的预计收入(没有去除手续费的预估收入)
                    } else {
                        predictedIncomeTv.setText(FormatUtil.formatMoney(predictedIncome) + "元" + handFeetStr);        //卖出联豆的预计收入(没有去除手续费的预估收入)
                    }
                } else {
                    predictedIncomeTv.setText(FormatUtil.formatMoney(0) + "元");        //卖出联豆的预计收入
                }
            }
        });
    }

简单详解:

获得两个EditText的 可被观察对象

获得两个edittext的可被观察的(observable<CharSequence>对象

        Observable<CharSequence> priceObservable =
RxTextView.textChanges(priceEdit.getEditText());
        Observable<CharSequence> countObservable = 
RxTextView.textChanges(countEdit.getEditText());

综合运算

动态算出 价格、数量是否合理

 sellBtnSubscription = Observable.combineLatest(priceObservable, countObservable,
                (priceSequence, countSequence) -> {
                    if (priceSequence.toString().startsWith(".")) {
                        priceSequence = "0" + priceSequence;
                        priceEdit.setText("0.");
                        priceEdit.getEditText().setSelection(priceEdit.getText().length());
                    }
                    double inputPrice = StringUtils.isEmpty(priceSequence.toString()) ? 0 : Double.parseDouble(priceSequence.toString());
                    int inputCount = StringUtils.isEmpty(countSequence.toString()) ? 0 : Integer.parseInt(countSequence.toString());
                    return inputPrice > 0 && inputCount > 0;
                }).subscribe(new Observer<Boolean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(Boolean isSellBtnEnable) {
                sellBtn.setEnabled(isSellBtnEnable);
                String handFeetStr = "(含手续费)";
                if (isSellBtnEnable) {
                    double predictedIncome = Double.parseDouble(priceEdit.getText().toString()) * Integer.parseInt(countEdit.getText());
                    predictedIncomeTv.setText(FormatUtil.formatMoney(predictedIncome) + "元");        //卖出XX币的预计收入(没有去除手续费的预估收入)
                    //( 含手续费 )
                    if (predictedIncome == 0) {
                        predictedIncomeTv.setText(FormatUtil.formatMoney(predictedIncome) + "元");        //卖出XX币的预计收入(没有去除手续费的预估收入)
                    } else {
                        predictedIncomeTv.setText(FormatUtil.formatMoney(predictedIncome) + "元" + handFeetStr);        //卖出XX币的预计收入(没有去除手续费的预估收入)
                    }
                } else {
                    predictedIncomeTv.setText(FormatUtil.formatMoney(0) + "元");        //卖出联豆的预计收入
                }
            }
        });
上一篇下一篇

猜你喜欢

热点阅读