如何在android中的多个Edittext中添加单个TextWatcher?

时间:2016-07-20 04:34:58

标签: android

我有两个编辑文本,即手机号码和密码,我在其中添加TextWatcher,但我想在多个编辑文本中使用单个TextWatcher。我该怎么做

TextWatcher mMobileWatcher = 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) {
        checkFieldsForEmpty();
    }

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

    @Override
    public void afterTextChanged(Editable s) {

        m_EmailId = m_Inputemail.getText().toString().trim();
        if (!isValidEmail(m_EmailId)) {
            m_Inputemail.setError("Please enter valid Email Id");
        }
    }
};
m_InputMobile = (EditText) findViewById(R.id.input_mobile);
    m_InputMobile.addTextChangedListener(mMobileWatcher);
m_Inputemail = (EditText) findViewById(R.id.input_emailId);
    m_Inputemail.addTextChangedListener(m_EmailWatcher);

3 个答案:

答案 0 :(得分:0)

尝试这种方式

private class CustomTextWatcher implements TextWatcher{

    private View view;
    private CustomTextWatcher (View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    public void afterTextChanged(Editable editable) {
        String text = editable.toString();
        switch(view.getId()){
            case R.id.edtname:
                model.setName(text);
                break;
            case R.id.edtemail:
                model.setEmail(text);
                break;
            case R.id.edtphone:
                model.setPhone(text);
                break;
        }
    }
}

在oncreate中使用

edtname = (EditText) findViewById(R.id.edtname);
edtname.setText(model.getName());
edtname.addTextChangedListener(new CustomTextWatcher (edtname));

edtemail = (EditText) findViewById(R.id.edtemail);
edtemail .setText(model.getEmail());
edtemail .addTextChangedListener(new CustomTextWatcher (edtemail));

edtphone = (EditText) findViewById(R.id.edtphone);
edtphone .setText(model.getPhone());
edtphone .addTextChangedListener(new CustomTextWatcher (edtphone ));

答案 1 :(得分:0)

你可以这样做:

    private final TextWatcher mTextWatcher = new TextWatcher() {

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    public void afterTextChanged(Editable s) {

        if (s == [youredittext].getEditableText()) {

            }
        if (s == [youranotheredittext].getEditableText()) {

        }
    }
};

在onCreate()

    [youredittext].addTextChangedListener(mTextWatcher);
    [youranotheredittext].addTextChangedListener(mTextWatcher);

答案 2 :(得分:0)

  

<强>的onCreate

   et1=(EditText)findViewById(R.id.et1);
   et2=(EditText)findViewById(R.id.et2);
   et3=(EditText)findViewById(R.id.et3);
   et4=(EditText)findViewById(R.id.et4);
  et1.addTextChangedListener(new MyTextWatcher(et1));
  et2.addTextChangedListener(new MyTextWatcher(et1));
  et3.addTextChangedListener(new MyTextWatcher(et1));
  et4.addTextChangedListener(new MyTextWatcher(et1));
  

MyTextWatcher接收您的课程

 private class MyTextWatcher implements TextWatcher {

    private View view;

    private MyTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void afterTextChanged(Editable editable) {
        switch (view.getId()) {
        case R.id.et1: // et1 is id of edittext et1;

        // put You Code


            break;
        case R.id.et2:


            break;

              case R.id.et3:


            break;

              case R.id.et4:


            break;
        }
    }
    }