So, I tried to implement TextWatcher for Android and ran into several problems when TextChangedListener was called several times or went into an infinite loop, because I want to convert the text in the EditText widget to a string with a formatted currency.
What I did to get around this was to create my own custom TextWatcher, and then the following happened in the afterTextChanged event:
public class CurrencyTextWatcher implements TextWatcher { private EditText et; public CurrencyTextWatcher(EditText editText) { et = editText; } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } public void afterTextChanged(Editable s) { et.removeTextChangedListener(this); et.setText(myCurrencyString); et.addTextChangedListener(this); } }
So my question is: is there a better way to do this? I want one kind of EditText Widget to hold where the changes go and the resulting formatted string.
Also, are there any other problems associated with removing and adding a TextChangedListener?
Thanks in advance
source share