在Android上如何为多个EditText添加文本监听器?

4

我有一个包含两个编辑框的活动,并且在该活动中有一个单独实现的TextWatcher。我想创建一个单独的类来实现TextWatcher,并在这些编辑框中使用它。我该如何做到这一点? 代码:-

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
3个回答

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);

或者创建一个自定义的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();
   }
}

更多详细信息请访问此问题

编程愉快。


1
  1. Multiple edittext Intialization & Setting TextChangeListener

    for(int i=0;i<editTexts_ids.length;i++)
    {
        editTexts[i] = (EditText)view.findViewById(editTexts_ids[i]);
        editTexts[i].addTextChangedListener(this);
    
        textInputLayouts[i]= (TextInputLayout)view.findViewById(textInputLayouts_ids[i]);
        //Log.e("EditText["+i+"]",""+editTexts[i].getId());
    }
    
  2. In afterTextChanged Method

    @Override 
    public void afterTextChanged(Editable editable) {
    
    for(int i=0;i<editTexts_ids.length;i++) // Disable Error AFter Entered Text
         {//not include Mobile Number Name,S/o, Nominee Edittexts
            if (editTexts[i].getText().hashCode() == editable.hashCode())// Checking
            {
                //Mobile Number VAlidation At Runtime
                if(i==11)
                {
                    if(Utils.Mobile_NumberValidation(editable.toString()))
                    {
                        textInputLayouts[i].setError("");
                        return;
                    }
                    else
                    {
                        textInputLayouts[i].setError("Invalid Mobile Number (should start with 7 or 8 or 9) , Length 10 digits");
                        return;
                    }
                }
                if (i == 4) { // for Name
                    if(!Utils.Name_Validation(editable.toString())) {
                        textInputLayouts[4].setError("Invalid Person Name (Special Symbols(@,!,$ .etc), DOT (.) are not allowed)");
                        return;
                    }else
                    {
                        textInputLayouts[4].setError("");
                        return;
                    }
                }
                if (i == 5) { //for S/o,D/o,W/o
                    if(!Utils.Name_Validation(editable.toString())) {
                        textInputLayouts[5].setError("Invalid Person Name (Special Symbols(@,!,$ .etc), DOT (.) are not allowed)");
                        return;
                    }else
                    {
                        textInputLayouts[5].setError("");
                        return;
                    }
                }
    
                if (i == 9) { //for S/o,D/o,W/o
                    if(!Utils.Name_Validation(editable.toString())) {
                        textInputLayouts[9].setError("Invalid Person Name (Special Symbols(@,!,$ .etc), DOT (.) are not allowed)");
                        return;
                    }else
                    {
                        textInputLayouts[9].setError("");
                        return;
                    }
                }
    
    
                if (!editable.toString().trim().isEmpty()) {
                    textInputLayouts[i].setError("");
                    return;
                }
    
            }
    
    }
    

    }


for(int i=0;i<editTexts_ids.length;i++) { editTexts[i]= (EditText) view.findViewById(editTexts_ids[i]); editTexts[i].addTextChangedListener(this);textInputLayouts[i]= (TextInputLayout) view.findViewById(textInputLayouts_ids[i]); //Log.e("EditText["+i+"]",""+editTexts[i].getId());} - Mallikarjuna

0
只需创建一个TextWatcher对象,并将其传递到addTextChangeListener中,如下所示:

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

    m_InputMobile.addTextChangedListener(textWatcher);

假设我有5个编辑文本框...那么我必须创建5个TextWatcher对象。 - vishwas
将其设置在第二个上,像这样:secondEditText.addTextChangedListener(textWatcher)。 - Talha Mir
只有一个这样的对象。您将该对象传递给不同的侦听器。 - Talha Mir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接