2

我试图实现 Textwatcher 接口,我试图更改 中的文本afterTextChanged(),但它崩溃了。为什么会崩溃?:

        @Override
        public void afterTextChanged(Editable s) {
            Editable myText;

            if(s.toString().contains("hi")){
                myText = s.append(" ho");
                myEdit.setText(myText);

            }
        }

日志猫

01-27 01:23:18.213: E/AndroidRuntime(2563): FATAL EXCEPTION: main
01-27 01:23:18.213: E/AndroidRuntime(2563): java.lang.StackOverflowError
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.graphics.Paint.getTextRunAdvances(Paint.java:1720)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.MeasuredText.addStyleRun(MeasuredText.java:164)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.MeasuredText.addStyleRun(MeasuredText.java:204)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.StaticLayout.generate(StaticLayout.java:281)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.DynamicLayout.reflow(DynamicLayout.java:284)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.DynamicLayout.access$000(DynamicLayout.java:35)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.DynamicLayout$ChangeWatcher.reflow(DynamicLayout.java:621)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.DynamicLayout$ChangeWatcher.onTextChanged(DynamicLayout.java:631)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:247)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at com.example.textfields.MainActivity$1.afterTextChanged(MainActivity.java:45)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView.sendAfterTextChanged(TextView.java:7108)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:8805)
01-27 01:23:18.213: E/AndroidRuntime(2563):     at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
4

2 回答 2

5

您应该在设置其文本之前调用您的removeTextChangedListener()EditText,并在更改文本后重新添加您的侦听器。否则afterTextChanged()会一直被调用,就像你已经注意到的那样。

于 2013-01-27T01:35:52.703 回答
1

你也可以试试这个:-

这也用于在更改后更改备用 editText 值。

public class MainActivity extends AppCompatActivity 
implements TextWatcher{

   EditText editText1, editText2;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       editText1 = findViewById(R.id.editText1);
       editText2 = findViewById(R.id.editText2);

       editText1.addTextChangedListener(this);
       editText2.addTextChangedListener(this);

      }

    @Override
    public void beforeTextChanged(CharSequence s, int start, 
           int 
           count, int after) {

    }

   @Override
   public void onTextChanged(CharSequence s, int start, int 
           before, int count) {

   }

  @Override
  public void afterTextChanged(Editable s) {

      if (getCurrentFocus() == editText1) {
         // is only executed if the EditText was directly 
            changed by the user.
          Log.e("check","getCurrentFocus() == editText1");

          editText2.removeTextChangedListener(this);

          editText2.setText(String.valueOf(s));

          editText2.addTextChangedListener(this);

       //            editText1.clearFocus();
      }

      if (getCurrentFocus() == editText2) {
          // is only executed if the EditText was directly 
             changed by the user
          Log.e("check","getCurrentFocus() == editText2");

          editText1.removeTextChangedListener(this);

          editText1.setText(String.valueOf(s));

          editText1.addTextChangedListener(this);

        //            editText2.clearFocus();
        }


   }

}
于 2019-08-14T07:32:22.030 回答