How to format edittext (android) -
i need format edittext
add dot.
when input more 3 numbers put dot after first number (like this: 1.000)
but when input have more 4 numbers put dot after second number (like this: 10.000)
i tried addtextchangedlistener
didn't work.
this algorithm add numbers @ end , move dot, next example:
you have @ begining 0.00 insert 1 , have 0.01 insert 5 , have 0.15 insert 9 , have 1.59
try modified code , adapt need.
youtedittext.addtextchangedlistener( new textwatcher() { boolean mediting = false; public void ontextchanged(charsequence s, int start, int before, int count) { } public void beforetextchanged(charsequence s, int start, int count, int after) { } public void aftertextchanged(editable s) { if(!mediting) { mediting = true; string digits = s.tostring().replaceall("\\d", ""); numberformat nf = new decimalformat( "#,##0.00" ); try{ string formatted = nf.format(double.parsedouble(digits)/100); s.replace(0, s.length(), formatted); } catch (numberformatexception nfe) { s.clear(); } mediting = false; } } });
other thing need put focus @ end of text, can next code:
youredittext.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if(hasfocus){ amount.setselection(amount.gettext().length()); } } });
i hope can you.
Comments
Post a Comment