我有三个EditText。当我更改第一个EditText值时,第三个EditText值将由TextWatcher更改。当我更改第三个EditText时,第一个EditText值将由TextWatcher更改。当我运行代码时,应用程序将崩溃。
这是我的密码

   qty = (EditText) findViewById(R.id.qty);
   rate = (EditText) findViewById(R.id.rate);
   amount = (EditText) findViewById(R.id.amount);

   qty.addTextChangedListener(new TextWatcher() {
       @Override
       public void onTextChanged(CharSequence s, int start, int before, int count) {
           // TODO Auto-generated method stub
       }

       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
           // TODO Auto-generated method stub
       }

       @Override
       public void afterTextChanged(Editable s) {
           // TODO Auto-generated method stub
           String qtyvalue = qty.getText().toString();
           String ratevalue = rate.getText().toString();

           if (qtyvalue.equals("") || ratevalue.equals("")) {
               val1 = 0d;
               val2 = 0d;
           } else {
               val1 = Double.parseDouble(qtyvalue);
               val2 = Double.parseDouble(ratevalue);
           }

           amount.setText(Double.toString(val1 * val2));
       }
   });

   amount.addTextChangedListener(new TextWatcher() {
       @Override
       public void onTextChanged(CharSequence s, int start, int before, int count) {
           // TODO Auto-generated method stub
       }

       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
           // TODO Auto-generated method stub
       }

       @Override
       public void afterTextChanged(Editable s) {
           // TODO Auto-generated method stub
           String amountvalue = amount.getText().toString();
           String ratevalue = rate.getText().toString();

           if (amountvalue.equals("") || ratevalue.equals("")) {
               val3 = 0d;
               val4 = 0d;
            } else {
               val3 = Double.parseDouble(amountvalue);
               val4 = Double.parseDouble(ratevalue);
            }

            System.out.println(Double.toString((double)val3/val4));
            qty.setText(Double.toString((double)val3/val4));
        }
    });

洛卡特
 java.lang.StackOverflowError
            at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
            at android.text.TextUtils.getChars(TextUtils.java:78)
            at android.text.TextUtils.indexOf(TextUtils.java:111)
            at android.text.StaticLayout.generate(StaticLayout.java:209)
            at android.text.DynamicLayout.reflow(DynamicLayout.java:324)
            at android.text.DynamicLayout.<init>(DynamicLayout.java:174)
            at android.widget.TextView.makeSingleLayout(TextView.java:6964)
            at android.widget.TextView.makeNewLayout(TextView.java:6813)
            at android.widget.TextView.checkForRelayout(TextView.java:7409)
            at android.widget.TextView.setText(TextView.java:4300)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$1.afterTextChanged(Delivery.java:93)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$2.afterTextChanged(Delivery.java:127)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$1.afterTextChanged(Delivery.java:93)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$2.afterTextChanged(Delivery.java:127)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$1.afterTextChanged(Delivery.java:93)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$2.afterTextChanged(Delivery.java:127)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$1.afterTextChanged(Delivery.java:93)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$2.afterTextChanged(Delivery.java:127)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView.java:4137)
            at android.widget.EditText.setText(EditText.java:104)
            at android.widget.TextView.setText(TextView.java:4112)
            at com.example.admin.foms.Delivery$1.afterTextChanged(Delivery.java:93)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:8293)
            at android.widget.TextView.setText(TextView.java:4307)
            at android.widget.TextView.setText(TextView

最佳答案

根据this answer,为了避免在更改文本时递归,可以将flag设置为afterTextChanged方法,如下所示:

 boolean preventUpdate = false;

   @Override
   public void afterTextChanged(Editable s) {
      if(preventUpdate){
         preventUpdate = false; // reset flag after calling
         return;
      }

     // your logic

        // prevent update after change value
        preventUpdate = true;
        qty.setText(Double.toString((double)val3/val4));
   }

如果两个EdiText更改彼此的文本,则应添加另一个标志,以确保它们不会再次通知文本更改(考虑您的逻辑,只要您想使用afterTextChanged方法)。

关于android - 使用Textwatcher时出现Stackoverflow错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34282110/

10-10 06:16