is this editext for credit card?
first create count variable
int count = 0;
then put this in your oncreate(activity) / onviewcreated(fragment)
ccEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) { /*Empty*/}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) { /*Empty*/ }
@Override
public void afterTextChanged(Editable s) {
int inputlength = ccEditText.getText().toString().length();
if (count <= inputlength && inputlength == 4 ||
inputlength == 9 || inputlength == 14)){
ccEditText.setText(ccEditText.getText().toString() + " ");
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
} else if (count >= inputlength && (inputlength == 4 ||
inputlength == 9 || inputlength == 14)) {
ccEditText.setText(ccEditText.getText().toString()
.substring(0, ccEditText.getText()
.toString().length() - 1));
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
}
count = ccEditText.getText().toString().length();
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…