我正在将金额文本从 EditText
转换为 double 格式,并尝试将 EditText
中的 ","
删除为执行我的计算的正确格式。我的代码有什么问题并且有堆栈溢出吗?
// 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);
最佳答案
您的TextWathcer
更改了它正在监视的文本,导致对监视程序的另一次调用,导致对文本的另一次更改,......等等,直到您用完调用方法的堆栈空间。
解决此问题的一种方法是,当您在观察程序内运行时,将 boolean
标志设置为 true
,如果该标志设置为终止该标志,则立即返回递归。在伪代码中:
boolean mIsInWatcher = false;
void onTextChanged(...) {
if (mIsInWatcher) return;
mIsInWatcher = true;
// text modifications here
mIsInWatcher = false;
}
关于java - EditText 中 TextWatcher 观察器中的 StackOverflow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21600250/