如何在android中的多个编辑文本上添加文本观察器?

vis*_*was 4 android android-textwatcher

我有一个活动,其中包含两个编辑文本,并且TextWatcher该活动中有一个单独实现的活动。我想创建一个实现该编辑文本的TextWatcherTextWatcher。我该如何执行该代码:-

private TextWatcher getmWatcher = 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) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkFieldsForEmpty();
    }
};
private TextWatcher mWatcher = 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) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkFieldsForEmpty();
    }
};
m_InputMobile = (EditText) findViewById(R.id.input_mobile);// finding Id of Mobile Number edit text
    m_InputMobile.addTextChangedListener(getmWatcher);
    m_InputPassword = (EditText) findViewById(R.id.input_password);// finding Id of assword editText
    m_InputPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);// defining password edit Tect Input type
Run Code Online (Sandbox Code Playgroud)

V-r*_*hit 5

有两种方法可以做到这一点。

第一的

TextWatcher textWatcher = 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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
              if (m_InputMobile.getText().hashCode() == s.hashCode()) {
                  checkFieldsForEmpty();
              }
              else if (m_InputPassword.getText().hashCode() == s.hashCode()) {
                  checkFieldsForEmpty();
              }
        }
    };

m_InputMobile = (EditText) findViewById(R.id.input_mobile);
m_InputMobile.addTextChangedListener(getmWatcher);
m_InputPassword = (EditText) findViewById(R.id.input_password);
m_InputPassword.addTextChangedListener(getmWatcher);
Run Code Online (Sandbox Code Playgroud)

或开设客户TextWatcher课程

第二

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    m_InputMobile = (EditText) findViewById(R.id.input_mobile);
    m_InputMobile.addTextChangedListener(new CustomTextWatcher(m_InputMobile));
    m_InputPassword = (EditText) findViewById(R.id.input_password);
    m_InputPassword.addTextChangedListener(new CustomTextWatcher(m_InputPassword));
}

private class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;

    public CustomTextWatcher(EditText e) { 
        mEditText = e;
    }

   @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) {
       checkFieldsForEmpty();
   }
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请访问问题。

快乐编码。