2

How can I include a dash after the 3rd and 6th digit from a phone number and also how can I delete that dash if I press backspace? I am assuming that i have to use:

editText.addTextChangedListener(new TextWatcher);

Can someone help me with this? This is not a duplicate question cause the one you have shown me dose not cover the deletion of the dash. If you copy that code and try it, you will see that if you press backspace after you have more than 3 digits, the cursor will freeze before the dash.

  • yes, you need to use textwatcher – Jyoti JK Dec 12 '17 at 11:52
  • This question is not a duplicate of what you have just shown. That code is inserting a dash in a phone number, but not deleting it when I press backspace. If you copy that code, when you press backspace after you have more than 3 digits, you cursor will stop before the dash. – Andrei Nechita Dec 12 '17 at 12:08
  • try this, `TextWatcher textWatcher=new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Override public void onTextChanged(CharSequence s, int start, int before, int count) {e.removeTextChangedListener(textWatcher); if(s.length()==3||s.length()==7) e.setText(s+"-"); e.addTextChangedListener(textWatcher); }@Override public void afterTextChanged(Editable s) {} };` – Jyoti JK Dec 12 '17 at 12:14
  • Who is "e" and who is "textWatcher"? – Andrei Nechita Dec 12 '17 at 12:30
  • e is your edittext and, use this textwatcher as, editText.addTextChangedListener(textWatcher); – Jyoti JK Dec 13 '17 at 07:44
  • Ok. Now I see. But i have the same problem. If i press backspace(on a virtual keyboard) the cursor stops at the dash character. – Andrei Nechita Dec 13 '17 at 12:48

0 Answers0