Guest User

Untitled

a guest
May 22nd, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package de.brudaswen.util.format;
  2.  
  3. import android.text.Editable;
  4. import android.text.TextWatcher;
  5.  
  6. /**
  7. * Format input inside {@link android.widget.EditText} and make character blocks.
  8. * <p>
  9. * <p>
  10. * IBAN: <code>XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX</code>
  11. * <p>
  12. * Credit card: <code>XXXX XXXX XXXX XXXX</code>
  13. *
  14. * @author Sven Obser (sven@brudaswen.de)
  15. */
  16. public class NumberBlockWatcher implements TextWatcher {
  17.  
  18. private static final int NO_MAX_CHARS = -1;
  19.  
  20. /**
  21. * Create default IBAN text watch:
  22. * <p>
  23. * Style: <code>XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX</code>
  24. */
  25. public static NumberBlockWatcher getIbanNumberBlockWatcher() {
  26. return new NumberBlockWatcher(4, ' ', true, 36);
  27. }
  28.  
  29. /**
  30. * Create default credit card text watch:
  31. * <p>
  32. * Style: <code>XXXX XXXX XXXX XXXX</code>
  33. */
  34. public static NumberBlockWatcher getCreditCardNumberBlockWatcher() {
  35. return new NumberBlockWatcher(4, ' ', false, 16);
  36. }
  37.  
  38. private final int size;
  39. private final char space;
  40. private final boolean toUpper;
  41. private final int maxChars;
  42.  
  43. /**
  44. * Create Iban text watcher with custom properties.
  45. *
  46. * @param size Size of the character blocks.
  47. * @param space Spacing character between blocks.
  48. * @param toUpper Change characters to uppercase.
  49. * @param maxChars Max number of chars (excluding space) or {@link #NO_MAX_CHARS} to disable.
  50. */
  51. public NumberBlockWatcher(int size, char space, boolean toUpper, int maxChars) {
  52. this.size = size + 1;
  53. this.space = space;
  54. this.toUpper = toUpper;
  55.  
  56. if (maxChars > NO_MAX_CHARS) {
  57. this.maxChars = maxChars + maxChars / size - 1;
  58. } else {
  59. this.maxChars = NO_MAX_CHARS;
  60. }
  61. }
  62.  
  63. @Override
  64. public void onTextChanged(CharSequence s, int start, int before, int count) {
  65. }
  66.  
  67. @Override
  68. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  69. }
  70.  
  71. @Override
  72. public void afterTextChanged(Editable s) {
  73. for (int i = 0; i < s.length(); ) {
  74. final char c = s.charAt(i);
  75.  
  76. if (isSpacePosition(i) && c != space) {
  77. // Insert missing space
  78. s.insert(i, String.valueOf(space));
  79. i++;
  80. } else if (!isSpacePosition(i) && c == space) {
  81. // Delete wrongly placed space and check position again
  82. s.delete(i, i + 1);
  83. } else if (toUpper && !isSpacePosition(i) && Character.isLowerCase(c)) {
  84. s.replace(i, i + 1, String.valueOf(Character.toUpperCase(c)));
  85. } else {
  86. // Everything all right
  87. i++;
  88. }
  89. }
  90.  
  91. if (maxChars > NO_MAX_CHARS && s.length() > maxChars) {
  92. s.delete(maxChars, s.length());
  93. }
  94. }
  95.  
  96. private boolean isSpacePosition(int i) {
  97. return (i + 1) % size == 0;
  98. }
  99. }
Add Comment
Please, Sign In to add comment