Get editable identifier in afterTextChanged event

I have an Activity that extends TextWatcher to detect changes in some EditTexts, so it implements:

public void afterTextChanged(Editable s) 

My question is: if there are several EditTexts with the installation .addTextChangedListener (this), how can I tell which one has been modified to reflect the Editable object in the afterTextChanged procedure?

+7
source share
3 answers

Another option, with fewer anonymous inner classes, would be to simply check the current focused View . If your TextWatcher application depends only on the changes made by the user during input , the changes will always take place in the View , which has the current focus. Calling getCurrentFocus() from anywhere within an Activity or Window will return the View that the user is targeting. From within a TextWatcher this will almost certainly be a specific instance of EditText .

Link to SDK documents

Hope this helps!

+11
source

There is one way to implement this without creating a TextWatcher object for each EditText , but I would not use it:

 protected void onCreate(Bundle savedInstanceState) { // initialization... EditText edit1 = findViewById(R.id.edit1); edit1.addTextChangedListener(this); EditText edit2 = findViewById(R.id.edit1); edit2.addTextChangedListener(this); } private static CharSequence makeInitialString(EditText edit) { SpannableStringBuilder builder = new SpannableStringBuilder(); builder.setSpan(edit, 0, 0, Spanned.SPAN_MARK_MARK); return builder; } public void afterTextChanged(Editable s) { EditText[] edits = s.getSpans( 0, s.length(), EditText.class ); if (edits.length != 1) { // this mustn't happen } // here changed EditText EditText edit = edits[0]; } 
+2
source

see TextWatcher for more than one EditText basically create their own class to handle the listener using a constructor that defines the edittext that you control and pass in the edittext that you assign.

0
source

All Articles