计算textwatcher在android中无法正常工作

时间:2014-07-21 06:14:50

标签: android android-layout android-inflate textwatcher

我做了一个Android活动,因为多个LinearLayouts在" plus"上膨胀。按钮,现在在每个布局中我有2个edittext和一个textView,我想做的edittexts和显示到textView的乘法,它的工作正常,但是当多个linearLayouts绑定时我很困惑,因为我想要所有的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();

    }
};

1 个答案:

答案 0 :(得分:0)

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