java-在EditText上使用TextWatcher在android中实时计算总数和总和?

java-在EditText上使用TextWatcher在android中实时计算总数和总和?,第1张

概述在这里,我想使用来自数据库和setText的默认值作为该值,并计算净费率和总计,否则,如果用户编辑费率或收取费用,我想根据该值计算净费率和总计即时的.这是我用于计算和显示值的代码.privatevoidshowItem(Stringjson){Stringitembarcode="";Stringitemdesc=

在这里,我想使用来自数据库和setText的默认值作为该值,并计算净费率和总计,否则,如果用户编辑费率或收取费用,我想根据该值计算净费率和总计即时的.

这是我用于计算和显示值的代码.

    private voID showItem(String Json) {    String itembarcode = "";    String itemdesc = "";    String weight = "";    String rate = "";    String making = "";    double wt = 0.0d;    double rt = 0.0d;    double mk = 0.0d;    try {        JsONObject JsonObject = new JsONObject(Json);        JsONArray result = JsonObject.getJsONArray(Parsebarcode.JsON_ARRAY);        JsONObject itemData = result.getJsONObject(0);        itembarcode = itemData.getString(Parsebarcode.KEY_barCODE);        itemdesc = itemData.getString(Parsebarcode.KEY_DESC);        weight = itemData.getString(Parsebarcode.KEY_WEIGHT);        rate = itemData.getString(Parsebarcode.KEY_RATE);        making = itemData.getString(Parsebarcode.KEY_MAKING);    } catch (JsONException e) {        e.printstacktrace();    }    //table started    tableLayout.LayoutParams tableParams = new tableLayout.LayoutParams(tableLayout.LayoutParams.MATCH_PARENT, tableLayout.LayoutParams.WRAP_CONTENT);    tableRow.LayoutParams rowParams = new tableRow.LayoutParams(tableRow.LayoutParams.MATCH_PARENT, tableRow.LayoutParams.WRAP_CONTENT, 1f);    rowParams.setmargins(16, 0, 16, 0);    tableLayout tableLayout = new tableLayout(AddInvEst.this);    tableLayout.setLayoutParams(tableParams);    tableRow newRow = new tableRow(AddInvEst.this);    newRow.setLayoutParams(tableParams);    TextVIEw barCode = new TextVIEw(AddInvEst.this);    barCode.setLayoutParams(rowParams);    barCode.setGravity(Gravity.CENTER);    TextVIEw itemDesc = new TextVIEw(AddInvEst.this);    itemDesc.setLayoutParams(rowParams);    itemDesc.setGravity(Gravity.CENTER);    TextVIEw weightline = new TextVIEw(AddInvEst.this);    weightline.setLayoutParams(new tableRow.LayoutParams(tableRow.LayoutParams.MATCH_PARENT, tableRow.LayoutParams.WRAP_CONTENT, 0.75f));    weightline.setGravity(Gravity.CENTER);    EditText rateAmount = new EditText(AddInvEst.this);    rateAmount.setLayoutParams(new tableRow.LayoutParams(tableRow.LayoutParams.MATCH_PARENT, tableRow.LayoutParams.WRAP_CONTENT, 0.5f));    rateAmount.setGravity(Gravity.CENTER);   // String rtAmt = rateAmount.getText().toString().trim();    EditText makingAmount = new EditText(AddInvEst.this);    makingAmount.setLayoutParams(new tableRow.LayoutParams(tableRow.LayoutParams.MATCH_PARENT, tableRow.LayoutParams.WRAP_CONTENT, 0.5f));    makingAmount.setGravity(Gravity.CENTER);    //String mkAmt = makingAmount.getText().toString().trim();    TextVIEw netRate = new TextVIEw(AddInvEst.this);    netRate.setLayoutParams(new tableRow.LayoutParams(tableRow.LayoutParams.MATCH_PARENT, tableRow.LayoutParams.WRAP_CONTENT, 0.5f));    netRate.setGravity(Gravity.CENTER);    TextVIEw itemtotal = new TextVIEw(AddInvEst.this);    itemtotal.setLayoutParams(rowParams);    itemtotal.setGravity(Gravity.CENTER);    wt = Double.parseDouble(weight);    rt = Double.parseDouble(rate);    mk = Double.parseDouble(making);    double NetRate = rt + mk;    double Total = (NetRate / 10) * wt;    barCode.setText(itembarcode);    itemDesc.setText(itemdesc);    weightline.setText(weight);    rateAmount.setText(rate);    makingAmount.setText(making);    netRate.setText(NetRate + "");    itemtotal.setText(Total + "");    newRow.addVIEw(barCode);    newRow.addVIEw(itemDesc);    newRow.addVIEw(weightline);    newRow.addVIEw(rateAmount);    newRow.addVIEw(makingAmount);    newRow.addVIEw(netRate);    newRow.addVIEw(itemtotal);    itemtable.addVIEw(newRow);}

Using this code I am able to calculate the net rate and total but i want to round the double value to 4 decimal points.Also how would i calculate the sum total of all the totals in real time.Any help is appreciated.Thanks in advance.
This is my xml code.

解决方法:

以下不是完整的代码,但是可以给出实现的思路.
您必须创建一种新的方法来计算和显示结果-

private voID calculateAndShow(long wt, long rt, long mk){        double NetRate = rt + mk;        double Total = (NetRate / 10) * wt;        // Change the value here        barCode.setText(itembarcode);        itemDesc.setText(itemdesc);        weightline.setText(weight);        rateAmount.setText(rate);        makingAmount.setText(making);        netRate.setText(NetRate + "");        itemtotal.setText(Total + "");    }

现在,将这些行添加到您的方法之外-

 private TextWatcher rateTextWatcher = new TextWatcher() {        @OverrIDe        public voID beforeTextChanged(CharSequence s, int start, int count, int after) {        }        @OverrIDe        public voID onTextChanged(CharSequence s, int start, int before, int count) {        }        @OverrIDe        public voID afterTextChanged(Editable s) {            String rate = rateAmount.getText().toString();            newRate = Double.parseDouble(rate);            calculateAndShow(wt, newRate, mk);        }    };    private TextWatcher makingAmountTextWatcher = new TextWatcher() {        @OverrIDe        public voID beforeTextChanged(CharSequence s, int start, int count, int after) {        }        @OverrIDe        public voID onTextChanged(CharSequence s, int start, int before, int count) {        }        @OverrIDe        public voID afterTextChanged(Editable s) {            String makingAmount = makingAmount.getText().toString();            newMK = Double.parseDouble(makingAmount);            calculateAndShow(wt, rt, newMK);        }    };

这些textwatcher就是这样的您的edittext-

EditText rateAmount = new EditText(AddInvEst.this);        rateAmount.setLayoutParams(new tableRow.LayoutParams(tableRow.LayoutParams.MATCH_PARENT, tableRow.LayoutParams.WRAP_CONTENT, 0.5f));        rateAmount.setGravity(Gravity.CENTER);        rateAmount.addTextChangedListener(rateTextWatcher); EditText makingAmount = new EditText(AddInvEst.this);        makingAmount.setLayoutParams(new tableRow.LayoutParams(tableRow.LayoutParams.MATCH_PARENT, tableRow.LayoutParams.WRAP_CONTENT, 0.5f));        makingAmount.setGravity(Gravity.CENTER);        makingAmount.addTextChangedListener(makingAmountTextWatcher);
总结

以上是内存溢出为你收集整理的java-在EditText上使用TextWatcher在android中实时计算总数总和?全部内容,希望文章能够帮你解决java-在EditText上使用TextWatcher在android中实时计算总数和总和?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1120531.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存