如何使用textchangedlistener在运行时从edittext中减去两个值

时间:2016-02-29 10:23:03

标签: android android-edittext

我想从EditText中减去两个值并显示在第三个EditText上,如果我对两个EditText中的任何一个进行更改,那么它必须反映在第三个EditText上。

我已经实现了一个代码,但它无法正常工作。

update table Column1 = ISNULL(Column1, '') + ISNULL(Column2, '')

2 个答案:

答案 0 :(得分:0)

试试这段代码。它为我工作

public class MainActivity extends Activity implements TextWatcher {

int iEditWages;
int iEditMoneySpent;
int iSubtract;
String sEditWages;
String sEditMoneySpent;
String sTotalLeftToSpend;
EditText EditWages;
EditText EditMoneySpent;
TextView TotalLeft;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditMoneySpent = (EditText) findViewById(R.id.TotalSpentNumber);
    EditWages = (EditText) findViewById(R.id.TotalWagesNumber);
    EditMoneySpent.addTextChangedListener(this);
    EditWages.addTextChangedListener(this);
}

@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) {

    EditWages = (EditText) findViewById(R.id.TotalWagesNumber);
    EditMoneySpent = (EditText) findViewById(R.id.TotalSpentNumber);
    TotalLeft = (TextView) findViewById(R.id.TotalViewNumber);

    sEditWages = EditWages.getText().toString();
    sEditMoneySpent = EditMoneySpent.getText().toString();
    try {
        iEditWages = Integer.parseInt(sEditWages);
        iEditMoneySpent = Integer.parseInt(sEditMoneySpent);
        iSubtract = iEditWages - iEditMoneySpent;
    } catch (NumberFormatException e) {
    }

    sTotalLeftToSpend = String.valueOf(iSubtract);
    TotalLeft.setText(sTotalLeftToSpend);

}

答案 1 :(得分:0)

在AfterTextChanged中更新此行:

sAmnt = amount.getText().toString();
sAmntp = amount.getText().toString();

为:

sAmnt = amount.getText().toString();
sAmntp = amountp.getText().toString();