Custom input phone number automatically add separators

Source: Internet
Author: User

A simple control, that is, add some logic to deal with, it seems to be directly monitoring, package up a convenient point

 Public classAccounttxtviewextendsAndroid.support.v7.widget.AppCompatEditText {Private Final CharCUT = '-';  PublicAccounttxtview (Context context) {Super(context); }     PublicAccounttxtview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicAccounttxtview (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Overrideprotected voidOnTextChanged (charsequence text,intStartintLengthbefore,intlengthafter) {        if(Text = =NULL|| Text.length () = = 0)            return; StringBuilder SB=NewStringBuilder ();  for(inti = 0; I < text.length (); i++) {//Add a separator            if(I! = 3 && I! = 8 && Text.charat (i) = =CUT) {                Continue; } Else{sb.append (Text.charat (i)); if((sb.length () = = 4 | | sb.length () = = 9)                        && Sb.charat (Sb.length ()-1)! =CUT) {Sb.insert (sb.length ()-1, CUT); }            }        }        //prevent setting values multiple times        if(!sb.tostring (). Equals (Text.tostring ())) {            intindex = start + 1; if(Sb.charat (start) = =CUT) {                if(Lengthbefore = = 0) {Index++; } Else{Index--; }            } Else {                if(Lengthbefore = = 1) {Index--;            }} setText (Sb.tostring ());        SetSelection (index); }Else{//Delete Time to JudgeString line = Text.subsequence (Text.length ()-1, Text.length ()). ToString (); if(Line.equals (string.valueof (CUT))) {//If the delete encounters the '-' symbol, the default removalSb.deletecharat (text.subsequence (0, Text.length ()-1). Length ());                SetText (Sb.tostring ());            SetSelection (Sb.length ()); }        }    }     Publicstring Getphone () {string result=NULL; String Val=GetText (). toString (); if(val = =NULL||val.isempty ())return""; Result= Val.replace (string.valueof (CUT), ""); returnresult; }}
View Code

Also easy to use

And then the effect comes out.

Enter the fourth when the '-' delimiter is automatically populated and then delete the '-' separator automatically

Custom input phone number automatically add separators

Related Article

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.