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