1. Customizing the EditText control public class divisionedittext extends edittext { /* length of each group */ private Integer length = 3; /* separators */ private String delimiter = ","; private String text = ""; public Divisionedittext (Context context) { super (Context); init (); } public divisionedittext (context context, attributeset attrs) { super (Context, attrs); init (); } public divisionedittext (Context context, attributeset attrs, int Defstyle) { super (Context, attrs, defstyle); init (); } /** Get each group number */ public integer getlength () { return length; } /** Set number of */ public void setlength per group (integer length) { this.length = length; } /** Get spacer */ public string getdelimiter () { return delimiter; } /** Set the spacer */ public void setdelimiter (string delimiter) { this.delimiter = delimiter; } public string getinputtext () { return Super.gettext (). toString (). Replace (Getdelimiter (), ""); } / ** * initialization */ public Void init () { // content change monitoring this.addtextchangedlistener (New divisiontextwatcher ()); // Get focus Monitoring This.setonfocuschangelistener (New divisionfocuschangelistener ()); } /** * Text Monitoring * * @author administrator * */ private class divisiontextwatcher implements textwatcher { @ Override public void aftertextchanged (Editable s ) { } @ Override public void beforetextchanged (CharSequence s, int start, int count, int after) { } @Override public voId ontextchanged (Charsequence s, int start, int before,int count) { // Statistic Number int len = s.length (); if (Len < getlength ())// length is less than the required number return; if (count > 1) { return; } //is inverted first, then inverted back text = inversiOnstring (Formatsymbol (inversionstring (s.tostring ()));//Key points settext (text); SetSelection (Text.length ()); } } /** * If so, remove it first, and then add */ after calculation private string formatsymbol (STRING&NBSP;STR) { char[] chars = str.replace (Getdelimiter (), ""). ToCharArray (); stringbuffer sb = new stringbuffer (); for (int i = 0; i < chars.length; i++) { if (I % geteachlength () == 0 && i != 0)// Each time you traverse to a multiple of 4, add a space { sb.append (GetDelimiter ()); sb.append (Chars[i]) ;// adding characters } else { sb.append (Chars[i]) ;// adding characters } } return sb.tostring (); } /** * Reverse String *&NBsp; * @param str * @return */ private string inversionstring (STRING&NBSP;STR) { char[] chars = str.tochararray (); stringbuffer sb = new stringbuffer (); for (int i= 0; i < chars.length; i++) { sb.append (chars[chars.length &NBSP;-&NBSP;I&NBSP;-&NBSP;1]); } return sb.tostring (); } /** * Get Focus Monitor * *&nbSP; @author Administrator * */ private class divisionfocuschangelistener implements onfocuschangelistener { @Override Public void onfocuschange (View v, boolean hasfocus) { if (Hasfocus) { // Set Focus setselection (GetText (). toString (). Length ()); } } } }
EditText "," when input is displayed