我尝试实现Textwatcher界面,我正在尝试更改afterTextChanged()
中的文本,但它崩溃了。为什么会崩溃? :
@Override
public void afterTextChanged(Editable s) {
Editable myText;
if(s.toString().contains("hi")){
myText = s.append(" ho");
myEdit.setText(myText);
}
}
logcat的
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)
答案 0 :(得分:4)
在设置文本之前,您应该在EditText
上调用removeTextChangedListener(),并在更改文本后添加回复者。否则afterTextChanged()
将继续被调用,就像你已经注意到的那样。
答案 1 :(得分: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();
}
}
}