上的EditText使用TextWatcher计算总和总和的实时机器人? [英] Using a TextWatcher on a EditText to calculate the total and sum total in real time in android?

查看:245
本文介绍了上的EditText使用TextWatcher计算总和总和的实时机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我想取默认值从我的数据库的setText来到该值并计算净率和总否则如果用户编辑的比率或制作费我想计算基于净率和总能够实时值

这是我的code,计算和显示的值。

 私人无效showItem(JSON字符串){
    字符串itembar code =;
    字符串itemdesc =;
    串重量=;
    字符串率=;
    字符串决策=;

    双重量= 0.0D;
    双RT = 0.0D;
    双MK = 0.0D;

    尝试 {
        的JSONObject的JSONObject =新的JSONObject(JSON);
        JSONArray结果= jsonObject.getJSONArray(ParseBar code.JSON_ARRAY);
        的JSONObject的ItemData = result.getJSONObject(0);
        itembar code = itemData.getString(ParseBar code.KEY_BAR code);
        itemdesc = itemData.getString(ParseBar code.KEY_DESC);
        体重= itemData.getString(ParseBar code.KEY_WEIGHT);
        率= itemData.getString(ParseBar code.KEY_RATE);
        制作= itemData.getString(ParseBar code.KEY_MAKING);
    }赶上(JSONException E){
        e.printStackTrace();
    }

    //表开始

    TableLayout.LayoutParams tableParams =新TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
    TableRow.LayoutParams rowParams =新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,1F);
    rowParams.setMargins(16,0,16,0);

    TableLayout tableLayout =新TableLayout(AddInvEst.this);
    tableLayout.setLayoutParams(tableParams);

    的TableRow NEWROW =新的TableRow(AddInvEst.this);
    newRow.setLayoutParams(tableParams);

    TextView的吧code =新的TextView(AddInvEst.this);
    酒吧code.setLayoutParams(rowParams);
    酒吧code.setGravity(Gravity.CENTER);

    TextView的itemDesc =新的TextView(AddInvEst.this);
    itemDesc.setLayoutParams(rowParams);
    itemDesc.setGravity(Gravity.CENTER);

    TextView的weightLine =新的TextView(AddInvEst.this);
    weightLine.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.75f​​));
    weightLine.setGravity(Gravity.CENTER);

    EditText上rateAmount =新的EditText(AddInvEst.this);
    rateAmount.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
    rateAmount.setGravity(Gravity.CENTER);
   //字符串rtAmt = rateAmount.getText()的toString()修剪()。

    EditText上makingAmount =新的EditText(AddInvEst.this);
    makingAmount.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
    makingAmount.setGravity(Gravity.CENTER);
    //字符串mkAmt = makingAmount.getText()的toString()修剪()。

    TextView的netRate =新的TextView(AddInvEst.this);
    netRate.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
    netRate.setGravity(Gravity.CENTER);

    TextView的itemtotal =新的TextView(AddInvEst.this);
    itemtotal.setLayoutParams(rowParams);
    itemtotal.setGravity(Gravity.CENTER);

    重量= Double.parseDouble(重量);
    RT = Double.parseDouble(率);
    MK = Double.parseDouble(制作);

    双NetRate = RT + MK;
    双总=(NetRate / 10)*重量;


    酒吧code.setText(itembar code);
    itemDesc.setText(itemdesc);
    weightLine.setText(重量);
    rateAmount.setText(率);
    makingAmount.setText(制作);
    netRate.setText(NetRate +);
    itemtotal.setText(共+);

    newRow.addView(酒吧code);
    newRow.addView(itemDesc);
    newRow.addView(weightLine);
    newRow.addView(rateAmount);
    newRow.addView(makingAmount);
    newRow.addView(netRate);
    newRow.addView(itemtotal);
    itemTable.addView(NEWROW);


}
 

  

使用这个code我能计算出净率和总的,但我想圆的双重价值到小数点后4 points.Also我怎么会总和所有总数的计算实际time.Any帮助AP是preciated.Thanks提前。       这是我的XML code。

解决方案

下面是不完整的code,但是会给实现的想法。 你必须创建一个计算的新方法,并显示结果 -

 私人无效calculateAndShow(长重量,长RT,长MK){
        双NetRate = RT + MK;
        双总=(NetRate / 10)*重量;

        //改变这里的值
        酒吧code.setText(itembar code);
        itemDesc.setText(itemdesc);
        weightLine.setText(重量);
        rateAmount.setText(率);
        makingAmount.setText(制作);
        netRate.setText(NetRate +);
        itemtotal.setText(共+);

    }
 

现在你的方法之外添加这些行 -

 私人TextWatcher rateTextWatcher =新TextWatcher(){
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){

        }

        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){

        }

        @覆盖
        公共无效afterTextChanged(编辑S){
            字符串率= rateAmount.getText()的toString()。
            newRate = Double.parseDouble(率);
            calculateAndShow(重量,newRate,MK);
        }
    };

    私人TextWatcher makingAmountTextWatcher =新TextWatcher(){
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){

        }

        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){

        }

        @覆盖
        公共无效afterTextChanged(编辑S){
            字符串makingAmount = makingAmount.getText()的toString()。
            newMK = Double.parseDouble(makingAmount);
            calculateAndShow(重量,室温,newMK);
        }
    };
 

和这些textwatcher您的EditText像这样的 -

 的EditText rateAmount =新的EditText(AddInvEst.this);
        rateAmount.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
        rateAmount.setGravity(Gravity.CENTER);
        rateAmount.addTextChangedListener(rateTextWatcher);


 EditText上makingAmount =新的EditText(AddInvEst.this);
        makingAmount.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT,0.5F));
        makingAmount.setGravity(Gravity.CENTER);
        makingAmount.addTextChangedListener(makingAmountTextWatcher);
 

Here i want to take the default value coming from my database and setText to that value and calculate the net rate and total or else if the user edits the rate or making charges i would like to calculate the net rate and total based on that value in real time.

This is my code for calculating and displaying the values.

    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.

解决方案

Below is not the complete code, but that will give an idea of the implementation. You have to create a new method for calculation and displaying the result -

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

    }

Now add these lines outside of your method -

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

And these textwatcher to your edittext like this one -

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

这篇关于上的EditText使用TextWatcher计算总和总和的实时机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆