Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* We create an interface with one method */
public interface TextWatcherWithInstance {
void onTextChanged(EditText editText, CharSequence s, int start, int before, int count);
}
/* We create a custom class called MultiTextWatcher.
* And pass the interface here
*/
public class MultiTextWatcher {
private TextWatcherWithInstance callback;
public MultiTextWatcher setCallback(TextWatcherWithInstance callback) {
this.callback = callback;
return this;
}
public MultiTextWatcher registerEditText(final EditText editText) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
callback.onTextChanged(editText, s, start, before, count);
}
@Override
public void afterTextChanged(Editable editable) {}
});
return this;
}
/*
* We can call this class from our Activity/Fragment like this:
* This only has one method, which we are using in the app
*/
new MultiTextWatcher()
.registerEditText(editText)
.setCallback(new MultiTextWatcher.TextWatcherWithInstance() {
@Override
public void onTextChanged(EditText editText, CharSequence s, int start, int before, int count) {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment