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

Posted

技术标签:

【中文标题】在 EditText 上使用 TextWatcher 在 android 中实时计算总数和总和?【英文标题】:Using a TextWatcher on a EditText to calculate the total and sum total in real time in android? 【发布时间】:2016-02-12 06:17:10 【问题描述】:

在这里,我想采用来自我的数据库的默认值并将 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);



使用此代码,我可以计算净费率和总计,但我想将双精度值四舍五入到小数点后 4 位。另外,我将如何实时计算所有总计的总和。感谢任何帮助。提前致谢。 这是我的 xml 代码。

【问题讨论】:

@Krishnabhadra 你能帮我解决这个问题吗我已经阅读了你在***.com/questions/5645332/… 上的帖子我正在尝试实现类似的东西。 【参考方案1】:

以下不是完整的代码,但可以让您了解实现。 您必须创建一个新的计算方法并显示结果 -

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);

【讨论】:

谢谢@Rahul Chaurasia 是的@Rahul Chaurasia 这解决了我的问题,非常感谢!

以上是关于在 EditText 上使用 TextWatcher 在 android 中实时计算总数和总和?的主要内容,如果未能解决你的问题,请参考以下文章

带有edittext的Listview - 在“下一个”上自动滚动

设置 EditText 光标颜色

我们如何在 EditText 边框上写文字? [复制]

从 JSON 字符串在 EditText 上显示 HTML

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

如果用户没有输入任何输入,如何在 Android 中的 Edittext 上设置占位符并使用它?