SOLID-接口隔离原理

时间:2019-06-25 10:51:44

标签: android interface

我想知道如何从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) {

        }
    });

2 个答案:

答案 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());
        }

    });