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;
}
}
});
}