Pregunta

I know we can add a TextWatcher to EditTexts using addTextChangedListener and remove it using removeTextChangedListener. But now I want to check if my EditText has any TextWatcher or not.

How can I do that?

I have a grid. It has an adapter. My adapter checks if the type of defined item is EditText add a TextWatcher to it. When method getView is called TextWatcher is added to that EditText. I want to avoid adding extra TextWatchers if one is added.

¿Fue útil?

Solución

Well there is work around , you can set tag to your edittext inside getview method using setTag method of View , before doing this check if getTag is not null if so you have already set TextWatcher...

if(editText object getTag is null then editText.seTtag and add watcher) else return

Otros consejos

There is no API to do this specifically. Why not maintain a boolean variable, say textWatch yourself?

Every time you set a watcher, set this to true and set it to false when the watcher is removed.

You can simply check this variable to see if the editText has a watcher or not.

You can use an array of booleans if you have more than one editText boxes that need to be checked.

You can create a Map to store all the EditText objects that have TextWatcher's

For example

Map<EditText, Boolean> textWatcherMap = new HashMap<>();

When you add the TextWatcher to the EditText, add its reference to the HashMap.

TextWatcher textWatcher = new TextWatcher(..);
EditText editText = (EditText) findViewById(R.id.editTextId);
editText.addTextChangedListener(textWatcher);
textWatcherMap.put(editText, true);

Then when you are working on the EditText, check the HashMap for the reference

if(textWatcherMap.get(editText) != null)
    useCurrentTextWatcher();
else 
{
    addNewTextWatcher();
    textWatcherMap.put(editText, true);
}

you can call

edittext.removeTextChangedListener(YourWatcherObject)

before every time you add a new watcher so that it can't have more than one watcher

edittext.addTextChangedListener(YourWatcherObject)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top