EditText "," when input is displayed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.