EditText Enter phone number automatically with space

Source: Internet
Author: User

Xml:

<edittext
Android:id= "@+id/edit_main"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:hint= "Please enter your phone number"
Android:inputtype= "Number"
Android:maxlength= "/>"

Activity:

Medittext = (EditText) Findviewbyid (R.id.edit_main);
Medittext.addtextchangedlistener (New Textwatcher () {
@Override
public void beforetextchanged (Charsequence s, int. start, int count, int after) {

}

@Override
public void ontextchanged (charsequence charsequence, int start, int before, int count) {
if (charsequence = = NULL | | charsequence.length () = = 0)
Return
StringBuilder StringBuilder = new StringBuilder ();
for (int i = 0; i < charsequence.length (); i++) {
if (i! = 3 && I! = 8 && Charsequence.charat (i) = = ") {
Continue
} else {
Stringbuilder.append (Charsequence.charat (i));
if ((stringbuilder.length () = = 4 | | stringbuilder.length () = = 9)
&& Stringbuilder.charat (Stringbuilder.length ()-1)! = ") {
Stringbuilder.insert (Stringbuilder.length ()-1, ');
}
}
}
if (!stringbuilder.tostring (). Equals (Charsequence.tostring ())) {
int index = start + 1;
if (Stringbuilder.charat (start) = = ") {
if (before = = 0) {
index++;
} else {
index--;
}
} else {
if (before = = 1) {
index--;
}
}
Medittext.settext (Stringbuilder.tostring ());
Medittext.setselection (index);
}
}

@Override
public void aftertextchanged (Editable s) {

}
});

EditText Enter phone number automatically with space

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.