我正在將我的金額文本從EditText
轉換爲雙格式,並試圖刪除EditText
中的","
,以便爲我的計算執行適當的格式。我的代碼有什麼問題,並有堆棧溢出?EditText中的TextWatcher觀察器中的StackOverflow錯誤
// Gets the two EditText controls' Editable values
String cleanBasicTax;
if(txtBasicTax.getText().toString().contains(",")){
cleanBasicTax=txtBasicTax.getText().toString().replace(",", "");
}
else{
cleanBasicTax=txtBasicTax.getText().toString();
}
String cleantxtSurcharge;
if(txtSurcharge.getText().toString().contains(",")){
cleantxtSurcharge=txtSurcharge.getText().toString().replace(",", "");
}
else{
cleantxtSurcharge=txtSurcharge.getText().toString();
}
String cleanInterest;
if(txtInterest.getText().toString().contains(",")){
cleanInterest=txtInterest.getText().toString().replace(",", "");
}
else{
cleanInterest=txtInterest.getText().toString();
}
String cleanCompromise=txtCompromise.getText().toString().replace(",", "");
if(txtCompromise.getText().toString().contains(",")){
cleanCompromise=txtCompromise.getText().toString().replace(",", "");
}
else{
cleanCompromise=txtCompromise.getText().toString();
}
Editable editableValueBasicTax = txtBasicTax.getText().replace(0, txtBasicTax.length(), cleanBasicTax),
editableValueSurcharge = txtSurcharge.getText().replace(0, txtSurcharge.length(), cleantxtSurcharge),
editableValueInterest = txtInterest.getText().replace(0, txtInterest.length(), cleanInterest),
editableValueCompromise = txtCompromise.getText().replace(0, txtCompromise.length(), cleanCompromise);
不知道爲什麼會發生stackoverflow,但此代碼需要審查肯定。爲什麼不聲明'final String comma =「,」;最後一個字符串爲空=「」;'和'字符串cleanBasicTax = txtBasicTax.getText().toString();''if(cleanBasicTax.contains(comma)){cleanBasicTax = cleanBasicTax.replace(comma,empty); } else {..}'等等.. –