How to Use Single Textwatcher for Multiple Edittexts

TextWatcher for more than one EditText

Suggested solution in @Sebastian Roth's answer is not one instance of TextWatcher for some EditTexts. It is one class and n instances of that class for n EditTexts.

Each EditText has its own Spannable. TextWatcher's events has this Spannable as s parameter. I check their hashCode (unique Id of each object). myEditText1.getText() returns that Spannable. So if the myEditText1.getText().hashCode() equals with s.hashCode() it means that s belongs to myEditText1

So if you want to have one instance of TextWatcher for some EditTexts you should use this:

private TextWatcher generalTextWatcher = new TextWatcher() {    

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

if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_onTextChanged(s, start, before, count);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_onTextChanged(s, start, before, count);
}
}

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

if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_beforeTextChanged(s, start, count, after);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_beforeTextChanged(s, start, count, after);
}
}

@Override
public void afterTextChanged(Editable s) {
if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_afterTextChanged(s);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_afterTextChanged(s);
}
}

};

and

myEditText1.addTextChangedListener(generalTextWatcher);
myEditText2.addTextChangedListener(generalTextWatcher);

TextWatcher for multiple EditText fields

Change your code as :

First of all you need to add textwatcher to particular EditText, which you didn't. Try as below :

yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

// TODO Auto-generated method stub
}

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

// TODO Auto-generated method stub
}

@Override
public void afterTextChanged(Editable s) {

// TODO Auto-generated method stub
}
});

And then,

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (registerNameInput.getText().toString().length() > 0
&& registerEmailInput.getText().toString().length() > 0
&& registerUsernameInput.getText().toString().length() > 0
&& registerPasswordInput.getText().toString().length() > 0) {
registerAction.setEnabled(true);
} else {
registerAction.setEnabled(false);
}
}

How to add Text Watcher on multiple Edit text in android?

There is two ways you can do this.

First

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

or make a custome TextWatcher class

Second

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

For more details visit this Question.

Happy Coding.

How to handle Single TextWatcher event for multiple EditTexts?

null your remaining editText on focus change

    et_first.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
et_second.setText("");
et_third.setText("");
}
});

et_second.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
et_first.setText("");
et_third.setText("");
}
});

et_third.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
et_second.setText("");
et_first.setText("");
}
});

Single TextWatcher for multiple EditTexts?

You can attach the same TextWatcher watch to each EditText. Depending on what you need to do you may need to create you implementation of TextWatcher with some context.



Related Topics



Leave a reply



Submit