Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mEdtPaymentCreditNumber.addTextChangedListener(new FourDigitCardFormatWatcher());
- public static class FourDigitCardFormatWatcher implements TextWatcher {
- private static final char space = ' ';
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- }
- @Override
- public void afterTextChanged(Editable s) {
- // Remove spacing char
- if (s.length() > 0 && (s.length() % 5) == 0) {
- final char c = s.charAt(s.length() - 1);
- if (space == c) {
- s.delete(s.length() - 1, s.length());
- }
- }
- // Insert char where needed.
- if (s.length() > 0 && (s.length() % 5) == 0) {
- char c = s.charAt(s.length() - 1);
- // Only if its a digit where there should be a space we insert a space
- if (Character.isDigit(c) && TextUtils.split(s.toString(), String.valueOf(space)).length <= 3) {
- s.insert(s.length() - 1, String.valueOf(space));
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment