【问题标题】:Calculation on textwatcher not working properly in android计算 textwatcher 在 android 中无法正常工作
【发布时间】:2014-07-21 06:14:50
【问题描述】:

我做了一个 android 活动,其中多个 LinearLayouts 在“加号”按钮上膨胀,现在在每个布局中我有 2 个 edittext 和一个 textView,我想将 edittexts 相乘并显示到 textView,它工作正常,但是当绑定多个线性布局时我很困惑,因为我希望最后所有 textview 值的总和,我尝试如下,但得到错误的值。

代码

add.setOnClickListener(new OnClickListener()){
@Override
onClick(){
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.raw_descs, null);
            ImageView buttonRemove = (ImageView) addView.findViewById(R.id.iv_del);
            et_item_id = (EditText) addView.findViewById(R.id.et_item_id);
            et_desc = (EditText) addView.findViewById(R.id.et_desc);
            et_qty = (EditText) addView.findViewById(R.id.et_qty);
            et_unit_prize = (EditText) addView.findViewById(R.id.et_unit_prize);
            et_amt = (EditText) addView.findViewById(R.id.et_amt);

            et_qty.addTextChangedListener(textwatcher);
            et_unit_prize.addTextChangedListener(textwatcher);

            buttonRemove.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    ((LinearLayout) addView.getParent()).removeView(addView);
                    // calculate();
                    if (cnt >= 0) {
                        cnt = cnt - 1;

                    }

                }
            });
            cnt = cnt + 1;
            listitems.setTag(cnt);

            listitems.addView(addView);
}

}
   private void calculateInvoice() {
    double QuantyInt = 1;
    double PriceInt = 0;
    double discountInt = 0;
    double shipInt = 0;
    for (int i = 0; i < listitems.getChildCount(); i++) {

        et_qty = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(4);
        et_amt = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(8);
        et_unit_prize = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(6);

        if (et_qty != null) {
            QuantyInt = Double.parseDouble(!et_qty.getText().toString().equals("") ? et_qty.getText().toString() : "0");

        }
        if (et_unit_prize != null) {
            PriceInt = Double.parseDouble(!et_unit_prize.getText().toString().equals("") ? et_unit_prize.getText().toString() : "0");

        }
        subtotal = (QuantyInt * PriceInt);

    }

    double textResult = subtotal;
    System.out.println("::::::::::::MY TOATAL PRICE::::::::::::::::>>>>>>>>>>>>>>>>" + subtotal);
    et_amt.setText("$" + textResult + "");

    double finaltotal = 0;

    for (int i = 0; i < subtotl.size(); i++) {
        System.out.println(":::::::::::::Subtotal values+++++++++++:::::::::::>>>>>>>>>>>>>>" + subtotl.get(i));
        finaltotal = finaltotal + Double.parseDouble(subtotl.get(i));

    }
    System.out.println(":::::::::::::Subtotal:::::::::::>>>>>>>>>>>>>>" + finaltotal);

    tv_pre.setText("$" + finaltotal + "");
    if (et_dis != null) {
        discountInt = Double.parseDouble(!et_dis.getText().toString().equals("") ? et_dis.getText().toString() : "0");

    }
    discount = ((finaltotal * discountInt) / 100);
    double txtdiscount = discount;
    tv_dis.setText("$" + txtdiscount + "");
    double totl1 = finaltotal - discount;
    tv_total.setText("$" + totl1 + "");

    if (et_ship != null) {
        shipInt = Double.parseDouble(!et_ship.getText().toString().equals("") ? et_ship.getText().toString() : "0");

    }
    tv_ship.setText("$" + et_ship.getText().toString());
    double fnltotl = (shipInt + totl1);
    tv_total.setText("$" + fnltotl + "");
}

TextWatcher textwatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        calculateInvoice();

    }
};

【问题讨论】:

    标签: android android-layout android-inflate textwatcher


    【解决方案1】:

    您不需要在 editText 中获取您再次为此注册 TextWatcher 的字符串。只需将 CharSequence 从 onTextChanged() 传递给 calculateInvoice(arg0) 并根据 arg0 进行计算。

    【讨论】:

    • 谢谢你亲爱的,但是你能告诉我改变我的代码吗..我请求你救救我..非常感谢
    • @user3820044:just 告诉你,实现 TextWatcher 的方式不是它应该的方式,所以我不知道你的计算逻辑是什么,什么是正确或错误的值。您可以逐行跟踪您的代码以跟踪每个值和计算。
    • Arsh-除了最后一个 Inflated Layouts 的编辑文本之外,我正在获得附加值,你能看看我的编辑吗?请
    猜你喜欢
    • 2014-05-29
    • 2019-12-16
    • 2017-02-04
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    相关资源
    最近更新 更多