Textwatcher Events Are Being Fired Multiple Times
Solution 1:
I had the same kind of problem, when I pressed backspace with cursor at the end of a continuous text, afterTextChange was called 3 times: - The first time with the correct s value - The second time with a clear value - The third time with the correct value again
After having search a lot on the web, I tried to change my EditText inputType to
android:inputType="textNoSuggestions"
Don't ask me why, but it worked, afterTextChanged is now called only once.
Solution 2:
booleanisOnTextChanged=false;
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
}
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
isOnTextChanged = true;
}
@OverridepublicvoidafterTextChanged(Editable quantity) {
if (isOnTextChanged) {
isOnTextChanged = false;
//dosomething
}
Solution 3:
According to the developer pages for TextWatcher, if a change is made to the Editable
within TextWatcher
, it will trigger further calls to all the TextWatchers
linked to that Editable
. Now, clearly your code doesn't trigger this behaviour.
However, it is quite possible that if, for whatever reason, the system has a TextWatcher
on the Editable
, the situation you describe can occur. "Why", I hear you cry, "should this happen?"
First, the classic defence: there is no reason for it not to happen and, strictly, app code should be written to be resilient to it.
Second, I can't prove it, but I could well imagine that the code which handles layout of the displayed text within an EditText
uses a TextWatcher
to handle updating the display of the text on the screen. This code might insert control codes (which you aren't shown) into the Editable
to ensure good line breaks and so on. It may even go round a loop a few times to get it right, and you might only get your first call after it has done all of its ...
EDIT
As per the comment by @Learn OpenGL ES, calling of a TextWatcher would be normal for things like autocorrect.
Solution 4:
I tried all the solution answered on this question, none of those worked for me. But after some search I found this post. Using the RxJava to make the debounce worked well for me. Here goes my final solution:
Add RxJava dependencies in the Gradle file:
compile'io.reactivex:rxandroid:1.0.1'compile'io.reactivex:rxjava:1.0.14'compile'com.artemzin.rxjava:proguard-rules:1.0.14.2'
Implement your subject:
PublishSubject<String> yourSubject = PublishSubject.create();
yourSubject .debounce(100, TimeUnit.MILLISECONDS)
.onBackpressureLatest()
.subscribe(s -> {
//Implements anything you want
});
Use your subject in your TextWatcher:
TextWatchermyTextWatcher=newTextWatcher() {
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
}
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
yourSubject.onNext(s.toString()); //apply here your subject
}
@OverridepublicvoidafterTextChanged(Editable s) {
}
};
Add the TextWatcher in the EditText listener:
my_edit_text.addTextChangedListener(myTextWatcher);
Solution 5:
u can use a boolean check, like:
inputBoxNumberEt.addTextChangedListener(newTextWatcher() {
booleanignoreChange=false;
@OverridepublicvoidafterTextChanged(Editable s) {
}
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@OverridepublicvoidonTextChanged(CharSequence s, int start,
int before, int count) {
if (!ignoreChange) {
///Do your checks
ignoreChange = true;
inputBoxNumberEt.setText(string);
inputBoxNumberEt.setSelection(inputBoxNumberEt.getText().length());
ignoreChange = false;
}
}
});
Post a Comment for "Textwatcher Events Are Being Fired Multiple Times"