android - 如何在android中的多个编辑文本上添加Text Watcher?

标签 android android-textwatcher

我有一个包含两个编辑文本的 Activity ,并且在该 Activity 中有一个单独实现的 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

最佳答案

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

首先

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问题。

快乐编码。

关于android - 如何在android中的多个编辑文本上添加Text Watcher?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177094/

相关文章:

Android AlertDialog异常 "Resources$NotFoundException"

android - 根据两个或多个 EditText 字段中的输入启用/禁用按钮的优雅方式

java - 从数据库搜索时 Android Activity 卡住

android - 如何将 getLongitude 或 getLatitude() 的值放入 EditText?

java - Android - 我可以以编程方式覆盖 simple_list_item_1 颜色吗?

jquery - 在 Android PhoneGap 应用程序中使用 JQTouch 打字时屏幕滚动?

c# - C#Xamarin形成了如何访问设备库并为自定义库 GridView 检索所有图像的方法

java - 文本观察者问题

android - TextWatcher 的 onTextChanged、beforeTextChanged 和 afterTextChanged 的​​区别