将两个edittext值一起添加并将结果显示在另一个edittext中

时间:2017-05-10 10:15:51

标签: android

我正在尝试将两个EditText值的值相加。我使用了另一个例子,其中我可以得到从另一个中减去一个EditText值并在EditText中显示该结果的结果。但是,我似乎无法弄清楚如何将两个加在一起并获得结果。当前两个EditText字段中包含值时,应自动计算结果。

我的代码如下:

type

“sTip”是从AmountReceived中减去totalCost的结果。因此,当用户键入交易的总成本,然后输入他们从客户收到的金额时,它会自动从客户计算他们的提示。

我正在尝试反过来,但我尝试的所有内容都会导致错误,或者只是无法正常工作。我希望如果用户输入totalCost和tip,它会显示AmountReceived。提前感谢任何可以指引我正确方向的人!

编辑:更新了代码

public void afterTextChanged(Editable s)
{
    eAmountReceived = (EditText) findViewById(R.id.amountReceived);
    eTotalCost = (EditText) findViewById(R.id.orderTotal);
    eTip = (EditText) findViewById(R.id.tip);
    eMileage = (EditText) findViewById(R.id.mileage);
    eGrandTotal = (EditText) findViewById(R.id.grandTotal);

    sAmountReceived = eAmountReceived.getText().toString();
    sTotalCost = eTotalCost.getText().toString();
    sMileage = eMileage.getText().toString();
    sTip = eTip.getText().toString();

    try
    {
        dAmountReceived = Double.parseDouble(sAmountReceived);
        dTotalCost = Double.parseDouble(sTotalCost);
        dSubtract = dAmountReceived - dTotalCost;
        dMileage = Double.parseDouble(sMileage);
        dGrandTotal = dSubtract + dMileage;


        dAdd = dTotalCost + dSubtract;


    } catch (NumberFormatException e){}

    sTip = String.valueOf(dSubtract);

    DecimalFormat df = new DecimalFormat("0.00");
    sTip = df.format(dSubtract);

    eTip.setText(sTip);

    sGrandTotal = String.valueOf(dGrandTotal);

    sGrandTotal = df.format(dGrandTotal);

    eGrandTotal.setText(sGrandTotal);
}

1 个答案:

答案 0 :(得分:1)

为每个edittext添加文本更改侦听器。例如,dTotalCost或dSubtract的侦听器将如下所示:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="boardindex_themefitted_board_holder">
        <div class="boardindex_themefitted_board_main">
            <div class="boardindex_themefitted_board_main_content">
                <div id="boardindex_themefitted_board_main_content_chunk1" class="show">
                    <div class="boardindex_themefitted_board_main_subject floatleft ellipsis">
                        <a href="http://default-smf.visualpulse.net/index.php?board=1.0" name="b1">General Discussion</a>
                    </div>
                    <div class="clear"></div>
                    <div class="boardindex_themefitted_board_main_description scroll_on_hover ellipsis" id="input" >
                        Feel free to talk about anything and everything in this board.Feel free to talk about anything and everything in this board.Feel free to talk about anything and everything in this board.
                    </div>
                    <div class="clear"></div>
                </div>

            </div>
        </div>
     
        <div class="clear"></div>
    </div>

编辑:添加界面TextWatcherand将其添加到editexts

dTotalCost.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {}

@Override    
public void beforeTextChanged(CharSequence s, int start,
 int count, int after) {
}

@Override    
public void onTextChanged(CharSequence s, int start,
 int before, int count) {
  if(s.length() != 0)
   dAdd = dTotalCost + dSubtract;
  }
});