Fcoder Fcoder - 1 year ago 147
Android Question

replace character inside TextWatcher in android

i use a TextWatcher to change pressed key value. my goal is that replace some characters while typing. for example when i type keys, if reached "S" character, replaces it with "a" character. my question is: should i do it in beforeTextChanged?? how? can anyone give me an example?

Answer Source

Using beforeTextChanged won't be useful because it won't interrupt the actual printing of the key to the EditText. I would use something similar to:

    public void afterTextChanged(Editable s) {
        if(s.length() > 0 && s.toString().charAt(s.length()-1) == 'S')
            final String newText = s.toString().substring(0, s.length()-1) + "a"; 

I added some toString()'s, not 100% sure how Editable works but I think that should cover it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download