Guest User

Untitled

a guest
Nov 22nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import android.support.annotation.StringDef
  2. import android.text.Editable
  3. import android.text.TextWatcher
  4. import android.widget.EditText
  5.  
  6. @StringDef(PHONE_9_MASK, PHONE_8_MASK, CPF_MASK, ZIP_CODE_PT_BR, MONTH_YEAR, CREDIT_CARD)
  7. @Retention(AnnotationRetention.SOURCE)
  8. annotation class MaskType
  9.  
  10. const val PHONE_9_MASK = "(##) #####-####"
  11. const val PHONE_8_MASK = "(##) ####-####"
  12. const val CPF_MASK = "###.###.###-##"
  13. const val ZIP_CODE_PT_BR = "#####-###"
  14.  
  15. const val MONTH_YEAR = "##/##"
  16.  
  17. const val CREDIT_CARD = "#### #### #### ####"
  18.  
  19. fun String.unmask(): String {
  20. return replace("[\\./\\(\\) \\-\\+]".toRegex(), "")
  21. }
  22.  
  23. @Suppress("UNUSED")
  24. fun EditText.insert(@MaskType mask: String): TextWatcher {
  25. val textWatcher = MaskTextWatcher(mask)
  26.  
  27. addTextChangedListener(textWatcher)
  28.  
  29. return textWatcher
  30. }
  31.  
  32. fun EditText.insertPhoneMask(): TextWatcher {
  33. val textWatcher = object : MaskTextWatcher() {
  34. override fun getMask(unmaskedValue: String): String {
  35. if (unmaskedValue.length < 11) {
  36. return PHONE_8_MASK
  37. }
  38.  
  39. return PHONE_9_MASK
  40. }
  41. }
  42.  
  43. addTextChangedListener(textWatcher)
  44.  
  45. return textWatcher
  46. }
  47.  
  48. fun String.formatPhone(): String {
  49. var _phone = this
  50.  
  51. if (length == 8)
  52. _phone = String.format("%s-%s", substring(0, 4), substring(4, length))
  53. else if (length == 9)
  54. _phone = String.format("%s-%s", substring(0, 5), substring(5, length))
  55. else if (length == 10)
  56. _phone = String.format("%s %s-%s", substring(0, 2), substring(2, 6), substring(6, length))
  57. else if (length == 11)
  58. _phone = String.format("%s %s-%s", substring(0, 2), substring(2, 7), substring(7, length))
  59.  
  60. return _phone
  61. }
  62.  
  63. open class MaskTextWatcher(val mask: String = ""): SimpleTextWatcher() {
  64.  
  65. internal var oldValue = ""
  66.  
  67. internal var isUpdating: Boolean = false
  68.  
  69. open fun getMask(unmaskedValue: String): String {
  70. return mask
  71. }
  72.  
  73. override fun afterTextChanged(edit: Editable) {
  74. val unmaskedString = edit.toString().unmask()
  75. val maskedString = StringBuilder("")
  76. val mask = getMask(unmaskedString)
  77.  
  78. // EditText was GC'ed
  79.  
  80. if (isUpdating) {
  81. oldValue = unmaskedString
  82. isUpdating = false
  83.  
  84. return
  85. }
  86.  
  87. var i = 0
  88.  
  89. for (m in mask.toCharArray()) {
  90. if (m != '#' && i < unmaskedString.length) {
  91. maskedString.append(m)
  92. continue
  93. }
  94.  
  95. try {
  96. maskedString.append(unmaskedString[i])
  97. } catch (e: Exception) {
  98. break
  99. }
  100.  
  101. i++
  102. }
  103.  
  104. isUpdating = true
  105.  
  106. edit.replace(0, edit.length, maskedString)
  107. }
  108. }
  109.  
  110. open class SimpleTextWatcher : TextWatcher {
  111. override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { }
  112.  
  113. override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { }
  114.  
  115. override fun afterTextChanged(edit: Editable) { }
  116. }
Add Comment
Please, Sign In to add comment