Android Bank card input Format program

Source: Internet
Author: User
Tags stringbuffer


We want to enter the bank card number in order to display in the format, rather than simply display a bunch of numbers, the following section of code to implement the bank card number format input.

Format of the bank card number
public void Bankcardnumaddspace (final edittext medittext) {
Medittext.addtextchangedlistener (New Textwatcher () {
int beforetextlength = 0;
int ontextlength = 0;
Boolean ischanged = false;

int location = 0;//where the cursor is recorded
Private char[] Tempchar;
Private StringBuffer buffer = new StringBuffer ();
int konggenumberb = 0;

@Override
public void beforetextchanged (charsequence s, int start, int count,
int after) {
Beforetextlength = S.length ();
if (buffer.length () > 0) {
Buffer.delete (0, Buffer.length ());
}
Konggenumberb = 0;
for (int i = 0; I < s.length (); i++) {
if (S.charat (i) = = "") {
konggenumberb++;
}
}
}

@Override
public void ontextchanged (charsequence s, int start, int before,
int count) {
Ontextlength = S.length ();
Buffer.append (S.tostring ());
if (ontextlength = = Beforetextlength | | ontextlength <= 3
|| ischanged) {
Ischanged = false;
Return
}
Ischanged = true;
}

@Override
public void aftertextchanged (Editable s) {
if (ischanged) {
Location = Medittext.getselectionend ();
int index = 0;
while (index < buffer.length ()) {
if (Buffer.charat (index) = = ") {
Buffer.deletecharat (index);
} else {
index++;
}
}

index = 0;
int KONGGENUMBERC = 0;
while (index < buffer.length ()) {
if ((index = = 4 | | index = = 9 | | index = = | | index = = 19)) {
Buffer.insert (Index, "");
konggenumberc++;
}
index++;
}

if (Konggenumberc > konggenumberb) {
Location + = (Konggenumberc-konggenumberb);
}

Tempchar = new Char[buffer.length ()];
Buffer.getchars (0, Buffer.length (), Tempchar, 0);
String str = buffer.tostring ();
if (location > str.length ()) {
Location = Str.length ();
else if (location < 0) {
Location = 0;
}

Medittext.settext (str);
Editable etable = Medittext.gettext ();
Selection.setselection (etable, location);
Ischanged = false;
}
}
});
}

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.