我有3个edittext字段。 (数量,单位成本和总数)。当第一个(数量)被填充时,如果接下来输入单位成本,它会自动计算总数,或者如果输入总数,它会自动计算单位成本。个别地,功能正常。然而,当我将它们组合时,我得到了堆栈溢出错误
JAVA代码:
unit.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) {
String qty = quantity.getText().toString();
if (qty.matches("")) {
} else {
total.setEnabled(false);
total.setFocusable(false);
int totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString());
total.setText(Integer.toString(totalcost));
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
total.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) {
String qty = quantity.getText().toString();
if(qty.matches("")){
} else{
unit.setEnabled(false);
unit.setFocusable(false);
int unitcost = Integer.parseInt(total.getText().toString()) / Integer.parseInt(qty) ;
unit.setText(Integer.toString(unitcost));
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
logcat的:
07-31 03:39:47.640 1623-1623/com.example.Prototype E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.Prototype, PID: 1623
java.lang.StackOverflowError
at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
at android.text.TextUtils.getChars(TextUtils.java:79)
at android.text.TextUtils.indexOf(TextUtils.java:112)
at android.text.StaticLayout.generate(StaticLayout.java:182)
at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
at android.text.DynamicLayout.<init>(DynamicLayout.java:170)
at android.widget.TextView.makeSingleLayout(TextView.java:6134)
at android.widget.TextView.makeNewLayout(TextView.java:6032)
at android.widget.TextView.checkForRelayout(TextView.java:6571)
at android.widget.TextView.setText(TextView.java:3813)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$3.onTextChanged(productsFragmentTab.java:79)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$4.onTextChanged(productsFragmentTab.java:105)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$3.onTextChanged(productsFragmentTab.java:79)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$4.onTextChanged(productsFragmentTab.java:105)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$3.onTextChanged(productsFragmentTab.java:79)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$4.onTextChanged(productsFragmentTab.java:105)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$3.onTextChanged(productsFragmentTab.java:79)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$4.onTextChanged(productsFragmentTab.java:105)
at android.widget.TextView.sendOnTextChanged(TextView.java:7408)
at android.widget.TextView.setText(TextView.java:3816)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.example.Prototype.productsFragmentTab$3.onTextChanged(productsFragmentTab.java:79
答案 0 :(得分:1)
尝试使用其他变量来定义此值更改来自代码。例如:
void yourMethod(){ unit.addTextChangedListener(new TextWatcher() { boolean isChangingByCode = false; @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 (isChangingByCode){ return; } String qty = quantity.getText().toString(); if (qty.matches("")) { } else { total.setEnabled(false); total.setFocusable(false); int totalcost = Integer.parseInt(qty) * Integer.parseInt(unit.getText().toString()); isChangingByCode = true; total.setText(Integer.toString(totalcost)); isChangingByCode = false; } } @Override public void afterTextChanged(Editable s) { } }); total.addTextChangedListener(new TextWatcher() { boolean isChangingByCode = false; @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 (isChangingByCode){ return; } String qty = quantity.getText().toString(); if(qty.matches("")){ } else{ unit.setEnabled(false); unit.setFocusable(false); int unitcost = Integer.parseInt(total.getText().toString()) / Integer.parseInt(qty) ; isChangingByCode = true; unit.setText(Integer.toString(unitcost)); isChangingByCode = false; } } @Override public void afterTextChanged(Editable s) { } }); }
答案 1 :(得分:0)
设置TextView
上 内TextWatcher
的文字可以保证召唤无限循环。发生这种情况是因为只要更改了TextView上的文本,就会调用已注册的TextWatcher
,然后再调用onTextChanged()
实现文本再次设置,等等。
要解决此问题,只需在修改文本之前删除TextWatcher
,然后再重新添加。这是一个例子:
@Override
public void afterTextChanged(Editable text) {
...
total.removeTextChangedListener(this);
// Modify text here
total.addTextChangedListener(this);
...
}
答案 2 :(得分:0)
尝试在int变量中保存值。
然后将其插入新的unitprice变量。
Int a =在这里获得价格;
Int b = get quntity;
Int totatl = b * a;
显示屏幕:总计
String是一个复杂的处理操作,然后你想避免在Parse操作中进行计算。将计算作为类型本身。