Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. public class PhoneNumberTextWatcher implements TextWatcher {
  2.  
  3. private DubaiEditText editText;
  4.  
  5. public PhoneNumberTextWatcher(DubaiEditText edTxtPhone) {
  6. this.editText = edTxtPhone;
  7. }
  8.  
  9. public void onTextChanged(CharSequence s, int cursorPosition, int before,
  10. int count) {
  11.  
  12. if (before == 0 && count == 1) { //Entering values
  13.  
  14. String val = s.toString();
  15. String a = "";
  16. String b = "";
  17. String c = "";
  18. if (val != null && val.length() > 0) {
  19. val = val.replace("-", "");
  20. if (val.length() >= 3) {
  21. a = val.substring(0, 3);
  22. } else if (val.length() < 3) {
  23. a = val.substring(0, val.length());
  24. }
  25. if (val.length() >= 6) {
  26. b = val.substring(3, 6);
  27. c = val.substring(6, val.length());
  28. } else if (val.length() > 3 && val.length() < 6) {
  29. b = val.substring(3, val.length());
  30. }
  31. StringBuffer stringBuffer = new StringBuffer();
  32. if (a != null && a.length() > 0) {
  33. stringBuffer.append(a);
  34.  
  35. }
  36. if (b != null && b.length() > 0) {
  37. stringBuffer.append("-");
  38. stringBuffer.append(b);
  39.  
  40. }
  41. if (c != null && c.length() > 0) {
  42. stringBuffer.append("-");
  43. stringBuffer.append(c);
  44. }
  45. editText.removeTextChangedListener(this);
  46. editText.setText(stringBuffer.toString());
  47. if (cursorPosition == 3 || cursorPosition == 7) {
  48. cursorPosition = cursorPosition + 2;
  49. } else {
  50. cursorPosition = cursorPosition + 1;
  51. }
  52. if (cursorPosition <= editText.getText().toString().length()) {
  53. editText.setSelection(cursorPosition);
  54. } else {
  55. editText.setSelection(editText.getText().toString().length());
  56. }
  57. editText.addTextChangedListener(this);
  58. } else {
  59. editText.removeTextChangedListener(this);
  60. editText.setText("");
  61. editText.addTextChangedListener(this);
  62. }
  63.  
  64. }
  65.  
  66. if (before == 1 && count == 0) { //Deleting values
  67.  
  68. String val = s.toString();
  69. String a = "";
  70. String b = "";
  71. String c = "";
  72.  
  73. if (val != null && val.length() > 0) {
  74. val = val.replace("-", "");
  75. if (cursorPosition == 3) {
  76. val = removeCharAt(val, cursorPosition - 1, s.toString().length() - 1);
  77. } else if (cursorPosition == 7) {
  78. val = removeCharAt(val, cursorPosition - 2, s.toString().length() - 2);
  79. }
  80. if (val.length() >= 3) {
  81. a = val.substring(0, 3);
  82. } else if (val.length() < 3) {
  83. a = val.substring(0, val.length());
  84. }
  85. if (val.length() >= 6) {
  86. b = val.substring(3, 6);
  87. c = val.substring(6, val.length());
  88. } else if (val.length() > 3 && val.length() < 6) {
  89. b = val.substring(3, val.length());
  90. }
  91. StringBuffer stringBuffer = new StringBuffer();
  92. if (a != null && a.length() > 0) {
  93. stringBuffer.append(a);
  94.  
  95. }
  96. if (b != null && b.length() > 0) {
  97. stringBuffer.append("-");
  98. stringBuffer.append(b);
  99.  
  100. }
  101. if (c != null && c.length() > 0) {
  102. stringBuffer.append("-");
  103. stringBuffer.append(c);
  104. }
  105. editText.removeTextChangedListener(this);
  106. editText.setText(stringBuffer.toString());
  107. if (cursorPosition == 3 || cursorPosition == 7) {
  108. cursorPosition = cursorPosition - 1;
  109. }
  110. if (cursorPosition <= editText.getText().toString().length()) {
  111. editText.setSelection(cursorPosition);
  112. } else {
  113. editText.setSelection(editText.getText().toString().length());
  114. }
  115. editText.addTextChangedListener(this);
  116. } else {
  117. editText.removeTextChangedListener(this);
  118. editText.setText("");
  119. editText.addTextChangedListener(this);
  120. }
  121.  
  122. }
  123.  
  124.  
  125. }
  126.  
  127. public void beforeTextChanged(CharSequence s, int start, int count,
  128. int after) {
  129. }
  130.  
  131. public void afterTextChanged(Editable s) {
  132.  
  133.  
  134. }
  135.  
  136. public static String removeCharAt(String s, int pos, int length) {
  137.  
  138. String value = "";
  139. if (length > pos) {
  140. value = s.substring(pos + 1);
  141. }
  142. return s.substring(0, pos) + value;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement