-1
我試圖將兩個EditText值的值加在一起。我使用了另一個例子,其中我可以得到從另一個EditText值中減去一個EditText值並將結果顯示在EditText中的結果。但是,我似乎無法弄清楚如何將兩者加在一起並得到結果。當前兩個EditText字段中有值時,應自動計算結果。將兩個edittext值加在一起並在另一個edittext中顯示結果
我的代碼如下:
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);
}
「STIP」 是從AmountReceived減去TOTALCOST的結果。因此,當用戶輸入交易的總成本,然後鍵入他們從客戶那裏收到的金額時,它會自動從客戶計算他們的提示。
我試圖做到這一點的反面,但我試過的一切都會導致錯誤,或者只是不起作用。我希望它是如果用戶鍵入totalCost和提示,它顯示AmountReceived。提前感謝任何能指引我朝着正確方向的人!
編輯:更新後的代碼
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
eAmountReceived.addTextChangedListener(this);
//eTotalCost.addTextChangedListener(this);
eTip.addTextChangedListener(this);
//eMileage.addTextChangedListener(this);
//eGrandTotal.addTextChangedListener(this);
eTip.addTextChangedListener(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)
{
if(eTip.isFocused() && isSet(eAmountReceived) && isSet(eTotalCost))
{
updateSubtract();
}
}
@Override
public void afterTextChanged(Editable s){}
});
eAmountReceived.addTextChangedListener(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)
{
if(eAmountReceived.isFocused() && isSet(eTotalCost) && isSet(eTip))
{
updateAdd();
}
}
@Override
public void afterTextChanged(Editable s){}
});
}
private void updateAdd()
{
double dTotalCost = Double.valueOf(eTotalCost.getText().toString());
double dTip = Double.valueOf(eTip.getText().toString());
eAmountReceived.setText(String.valueOf(dTotalCost + dTip));
}
private void updateSubtract()
{
double dAmountReceived = Double.valueOf(eAmountReceived.getText().toString());
double dTotalCost = Double.valueOf(eTotalCost.getText().toString());
eTip.setText(String.valueOf(dAmountReceived - dTotalCost));
}
private boolean isSet(EditText editText)
{
return !editText.getText().toString().matches("");
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
updateAdd();
updateSubtract();
}
@Override
public void afterTextChanged(Editable editable) {
}
謝謝你的快速反應!對不起,我一整天都在工作。如果我將其實現到我的代碼中,它會在我的onCreate方法中執行嗎?我嘗試過使用它,但它似乎只在將textChangedListener設置爲我的totalCost editText時才起作用。另外,如果我將這段代碼放入我的onCreate方法中,並保留原來的代碼的其餘部分,則應用程序崩潰或無法正常工作。我猜是因爲我在另一個計算中使用了dSubtract? –
首先將您的EditText初始化移動到onCreate,因爲以這種方式初始化它是錯誤的。每個edittext都應該在onCreate中。然後實現textListener並將其添加到每個editext。在上面的答案中查看我的編輯。這應該是你正在尋找的解決方案。只要確定你真的想聽聽哪個領域。 – tompadre
謝謝你的幫助。我用添加和減少方法更新了我的代碼,我認爲這樣做會做到這一點,但現在我得到一個NullPointerException,並且我的應用程序崩潰了。我不知道我做錯了什麼。我會接受你的回答,指出我在正確的方向,但我不明白我在哪裏得到例外。 –