繁体   English   中英

EditText中TextWatcher watcher中的StackOverflow错误

[英]StackOverflow error in TextWatcher watcher in EditText

我将数量文本从EditText转换为双EditText格式,并尝试在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;
}

如果您要调用textView onTextChangeListener并以相同的方法替换文本,则只需进入循环,因为replace将调用相同的方法。 因此,错误。

尝试替换其他地方的文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM