Is it possible to auto insert characters into an EditText
as the user inputs data?
I.e. if the user is entering a long number such as 123456789012
, is it possible for this number to appear as he is typing it in the edit text box, but with a dash every 4th character?
So as you type the number above you would see it being entered in the EditText
box but would look like this: 1234-5678-9012.
Currently I have an app where you can enter a long number and then press a button and it inserts the dashes for you, but I'm curious if it could be done as you type?
Many thanks for any help.
This is what I used
Here you replace delimiter with what you want to separate digits.
For those still facing trouble with backspace and multiple hyphens -
By tagging android, I think you are discussing about android editText, is so you can do it by listening the TextChangedListener,
EDITED: for backspace
This will do as well, only this code will insert
" - "
after 3rd character.to solve this issue, i write a class "AutoAddTextWatcher" :
1. Auto insert text into EditText.
2. insert text into EditText at positions you are setted.
3. delete text in EditText at positions you are setted, when text length bigger than 1.
code snippet :
AutoAddTextWatcher class
complete demo source :
https://github.com/henrychuangtw/AutoInsertEditText