I want to implement the TextWatcher
interface for more than one EditText
fields. Currently I am using :
text1.addTextChangedListener(this);
text2.addTextChangedListener(this);
then overriding the methods in my Activity:
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// do some operation on text of text1 field
// do some operation on text of text2 field
}
However this is working fine but I'm looking for other ways so that I can explicitly identify that in which EditText
field the SoftKeyboard
is currently focused.
I would do it like this:
I implemented it as:
and:
Yes, you could use multiple instances of a custom
TextWatcher
that store theTextView
. (TextView
is actually the class that hasaddTextChangedListener
.)Similar to the hashCode solution above you can just check if
getText()==s
. Instead of either storing all your controls orfindViewById
multiple times, you could simply scan the content tree yourself once for the control that has theCharSequence
.Of course you could also use
findTextView
insidebeforeTextChanged
andonTextChanged
.using "CustomTextWatcher" idea, I done that
1) Crated a new TextWatcherListener interface:
2)Created and used EditTextExtended instead of EditText (in my case):
3) So, where you need write this code:
4)use them:
Well, let me know what do you think.
You can always define
TextWatcher
as a parameter toaddTextChangedListener
method.This way you can have multiple definitions for each edit text.After try several ways to achieve this, i find the right way using
EditText.isFocused()
to distinguish one to another. For example: