【问题标题】:How to add Text Watcher on multiple Edit text in android?如何在android中的多个编辑文本上添加文本观察器?
【发布时间】:2016-11-05 17:39:47
【问题描述】:

我有一个包含两个编辑文本的活动,并且在该活动中有一个TextWatcher,它是单独实现的。我想创建一个实现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

【问题讨论】:

    标签: android android-textwatcher


    【解决方案1】:

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

    首先

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

    更多详情请访问this问题。

    快乐编码。

    【讨论】:

      【解决方案2】:
      1. 多个edittext初始化&设置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. 在 afterTextChanged 方法中

        @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
      【解决方案3】:

      只需创建一个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 对象
      • 也将它设置在第二个上,就像这样 secondEditText.addTextChangedListener(textWatcher)
      • 只有一个这样的对象。您在不同的侦听器中传递那个对象。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多