我想知道如何从SOLID中为TextWatcher实施接口隔离原理。
更具体:如何删除不需要的功能:
beforeTextChanged(), afterTextChanged()
我只需要:
onTextChanged()
passwordinput.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) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 0 :(得分:1)
创建一个继承自NewClass
的类MyTextWatcher
和
passwordinput.addTextChangedListener(new New Class() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
});
答案 1 :(得分:0)
您可以通过实现TextWatcher来创建自己的TextWatcher类,并将默认实现留空。然后在您的匿名实现中,仅重写onTextChanged()。
class MyTextWatcher implements 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) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
用法:
passwordinput.addTextChangedListener(new MyTextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
presenter.validateCredentials(emailinput.getText().toString(), passwordinput.getText().toString());
}
});