Skip to content

Instantly share code, notes, and snippets.

@Shujito
Created October 9, 2019 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shujito/13fe5999fc90863a8af97a5f20ff453c to your computer and use it in GitHub Desktop.
Save Shujito/13fe5999fc90863a8af97a5f20ff453c to your computer and use it in GitHub Desktop.
Utilidad para usar TextWatcher como lambda en TextView::addTextChangedListener
TextView textView = new TextView(this);
textView.addTextChangedListener(new TextWatcherAdapter() {
@Override
public void action(Action action, CharSequence charOrEditable, int start, int count, int afterOrBefore) {
if (action == Action.afterTextChanged) {
// magia verde
}
}
});
TextView textView = new TextView(this);
textView.addTextChangedListener((TextWatcherAdapter) (action, charOrEditable, start, count, afterOrBefore) -> {
if (action == TextWatcherAdapter.Action.afterTextChanged) {
// magia verde
}
});
public interface TextWatcherAdapter extends TextWatcher {
enum Action {
beforeTextChanged,
onTextChanged,
afterTextChanged
}
default void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.action(Action.beforeTextChanged, s, start, count, after);
}
default void onTextChanged(CharSequence s, int start, int before, int count) {
this.action(Action.onTextChanged, s, start, count, before);
}
default void afterTextChanged(Editable editable) {
this.action(Action.afterTextChanged, editable, 0, 0, 0);
}
void action(Action action, CharSequence charOrEditable, int start, int count, int afterOrBefore);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment