Guest User

Untitled

a guest
Apr 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. mEdtPaymentCreditNumber.addTextChangedListener(new FourDigitCardFormatWatcher());
  2.  
  3. public static class FourDigitCardFormatWatcher implements TextWatcher {
  4.  
  5. private static final char space = ' ';
  6.  
  7. @Override
  8. public void onTextChanged(CharSequence s, int start, int before, int count) {
  9. }
  10.  
  11. @Override
  12. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  13. }
  14.  
  15. @Override
  16. public void afterTextChanged(Editable s) {
  17. // Remove spacing char
  18. if (s.length() > 0 && (s.length() % 5) == 0) {
  19. final char c = s.charAt(s.length() - 1);
  20. if (space == c) {
  21. s.delete(s.length() - 1, s.length());
  22. }
  23. }
  24. // Insert char where needed.
  25. if (s.length() > 0 && (s.length() % 5) == 0) {
  26. char c = s.charAt(s.length() - 1);
  27. // Only if its a digit where there should be a space we insert a space
  28. if (Character.isDigit(c) && TextUtils.split(s.toString(), String.valueOf(space)).length <= 3) {
  29. s.insert(s.length() - 1, String.valueOf(space));
  30. }
  31. }
  32. }
  33. }
Add Comment
Please, Sign In to add comment