更新时间:2023-02-26 18:39:18
这个 java 类实现了一个 TextWatcher
来观察"你的编辑文本,观察对文本所做的任何更改:
This java class implements a TextWatcher
to "watch" your edit text, watching any changes done to the text:
public abstract class TextValidator implements TextWatcher {
private final TextView textView;
public TextValidator(TextView textView) {
this.textView = textView;
}
public abstract void validate(TextView textView, String text);
@Override
final public void afterTextChanged(Editable s) {
String text = textView.getText().toString();
validate(textView, text);
}
@Override
final public void
beforeTextChanged(CharSequence s, int start, int count, int after) {
/* Needs to be implemented, but we are not using it. */
}
@Override
final public void
onTextChanged(CharSequence s, int start, int before, int count) {
/* Needs to be implemented, but we are not using it. */
}
}
并且在您的 EditText
中,您可以将该文本观察器设置为其侦听器
And in your EditText
, you can set that text watcher to its listener
editText.addTextChangedListener(new TextValidator(editText) {
@Override public void validate(TextView textView, String text) {
/* Insert your validation rules here */
}
});