Consider:
TextView textView = new TextView(context);
textView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
s.append("A");
}
});
If we add a TextWatcher
to a TextView
, and I want to append a letter to this TextView
, every time the user writes a letter in it, but this keeps calling the TextWatcher
Listener, so on to StackOverFlow error
, so how can I append text without calling the TextWatcher
Listener again?
public class MyTextWatcher implements TextWatcher { private EditText editText; // Pass the EditText instance to TextWatcher by constructor public MyTextWatcher(EditText editText) { this. editText = editText; } @Override public void afterTextChanged(Editable e) { // Unregister self before update editText.
This is the interface for text whose content and markup can be changed (as opposed to immutable text like Strings). If you make a DynamicLayout of an Editable, the layout will be reflowed as the text is changed.
EditText is used for entering and modifying text. While using EditText width, we must specify its input type in inputType property of EditText which configures the keyboard according to input. EditText uses TextWatcher interface to watch change made over EditText.
Another way to avoid a stack overflow:
TextView textView = new TextView(context);
textView.addTextChangedListener(new TextWatcher() {
boolean editing = false;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (!editing){
editing = true;
s.append("A");
editing = false;
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With