Created
July 28, 2017 13:46
-
-
Save kikermo/549b06b33d7468444f1470e657281db9 to your computer and use it in GitHub Desktop.
Helper class to use TextWatcher with lambdas, providing any possible combination.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.kikermo.util; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
public class TextWatcherWrapper implements TextWatcher { | |
private OnTextChanged onTextChanged; | |
private AfterTextChange afterTextChange; | |
private BeforeTextChanged beforeTextChanged; | |
public TextWatcherWrapper(OnTextChanged onTextChanged, AfterTextChange afterTextChange, BeforeTextChanged beforeTextChanged) { | |
this.onTextChanged = onTextChanged; | |
this.afterTextChange = afterTextChange; | |
this.beforeTextChanged = beforeTextChanged; | |
} | |
public TextWatcherWrapper(OnTextChanged onTextChanged, AfterTextChange afterTextChange) { | |
this.onTextChanged = onTextChanged; | |
this.afterTextChange = afterTextChange; | |
} | |
public TextWatcherWrapper(OnTextChanged onTextChanged) { | |
this.onTextChanged = onTextChanged; | |
} | |
public TextWatcherWrapper(OnTextChanged onTextChanged, BeforeTextChanged beforeTextChanged) { | |
this.onTextChanged = onTextChanged; | |
this.beforeTextChanged = beforeTextChanged; | |
} | |
public TextWatcherWrapper(AfterTextChange afterTextChange, BeforeTextChanged beforeTextChanged) { | |
this.afterTextChange = afterTextChange; | |
this.beforeTextChanged = beforeTextChanged; | |
} | |
public TextWatcherWrapper(AfterTextChange afterTextChange) { | |
this.afterTextChange = afterTextChange; | |
} | |
public TextWatcherWrapper(BeforeTextChanged beforeTextChanged) { | |
this.beforeTextChanged = beforeTextChanged; | |
} | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
if (beforeTextChanged != null) | |
beforeTextChanged.beforeTextChanged(s, start, count, after); | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
if (onTextChanged != null) | |
onTextChanged.onTextChanged(s, start, before, count); | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
if (afterTextChange != null) | |
afterTextChange.afterTextChanged(s); | |
} | |
public interface OnTextChanged { | |
void onTextChanged(CharSequence s, int start, int before, int count); | |
} | |
public interface AfterTextChange { | |
void afterTextChanged(Editable s); | |
} | |
public interface BeforeTextChanged { | |
void beforeTextChanged(CharSequence s, int start, int count, int after); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mTextView.addTextChangedListener(new TextWatcherWrapper(s -> {/**Do somethign for afterTextChanged**/})); | |
mTextView.addTextChangedListener(new TextWatcherWrapper(s -> {/**afterTextChanged**/}, | |
(BeforeTextChanged)(s, start, before, count) -> {/**beforeTextChanged**/} | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment