EditText phone number format input, delete case

Source: Internet
Author: User

When we enter a phone number, we usually cut a longer phone number. This kind of words effect will be very much better. The monitoring of edittext can easily achieve this requirement. Just need us to the corresponding EditText plus a listener on the OK. Put on a watcher I wrote.

public static class Mytextwatcher implements Textwatcher {private EditText numberedittext;private Button ConfirmButton; int Beforelen = 0;int Afterlen = 0;public mytextwatcher (EditText numberedittext, Button ConfirmButton) {This.numbereditte XT = Numberedittext;this.confirmbutton = ConfirmButton;} @Overridepublic void OnTextChanged (charsequence s, int start, int before,int count) {} @Overridepublic void Beforetextchan Ged (charsequence s, int start, int count,int after) {Beforelen = S.length ();} @Overridepublic void aftertextchanged (Editable s) {String txt = numberedittext.gettext (). toString (); Afterlen = Txt.length (); if (Afterlen > Beforelen) {if (txt.length () = = 4 | | txt.length () = = 9) {Numberedittext.settext (new STRINGB Uffer (TXT). Insert (Txt.length ()-1, ""). ToString ()), Numberedittext.setselection (Numberedittext.gettext (). Length () );}} else {if (Txt.startswith (")") {Numberedittext.settext (new StringBuffer (TXT). Delete (afterLen-1, Afterlen). ToString () ); Numberedittext.setselection (numberEdittext.gettext (). Length ());}}} 


It only needs new watcher when it is used. Passing in the current edittext can

EditText phone number format input, delete case

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.