Last active
October 12, 2022 16:01
-
-
Save userddssilva/202293268046b6569e3919366596982e to your computer and use it in GitHub Desktop.
Este gist contém uma mascara de valores monetários usados no brasil, foi implementada em kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.uea.transirie.mypay.mylaundry.util | |
import android.text.Editable | |
import android.text.TextWatcher | |
import android.widget.EditText | |
class MaskBrMonetaryValue { | |
companion object { | |
fun mask(editableText: EditText): TextWatcher { | |
return object : TextWatcher { | |
private fun clearInputText(s: CharSequence?): String { | |
return s.toString() | |
.replace(",", "") | |
.replace(".", "") | |
} | |
private fun insertChar(c: Char, s: String, position: Int): String { | |
return s.substring(0, position) + c + s.substring(position, s.length) | |
} | |
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | |
if (s.toString().isEmpty()) | |
return | |
// extrai virgula e pontos | |
var formatedValue = clearInputText(s) | |
if (formatedValue.length < 3) { | |
formatedValue = "0$formatedValue" | |
} | |
if (formatedValue.length >= 3) { | |
// caso a entrada tenha os valores de zeros iniciais | |
// remove-se os zeros a esquerda enquanto se insere valores | |
if (s.toString().length == 5 && formatedValue[0] == '0') { | |
formatedValue = formatedValue.drop(1) | |
} | |
formatedValue = formatedValue.reversed() | |
// adiciona virgula de casa decimal | |
formatedValue = insertChar(',', formatedValue, 2) | |
// adiciona pontos de grandezas | |
if (formatedValue.length > 6) { | |
for (i in 6 until s.toString().length step 4) { | |
if (i != formatedValue.length) { | |
formatedValue = insertChar('.', formatedValue, i) | |
} | |
} | |
} | |
formatedValue = formatedValue.reversed() | |
} | |
editableText.removeTextChangedListener(this) | |
editableText.setText(formatedValue) | |
editableText.setSelection(formatedValue.length) | |
editableText.addTextChangedListener(this) | |
} | |
override fun afterTextChanged(s: Editable?) {} | |
override fun beforeTextChanged( | |
charSequence: CharSequence, | |
i: Int, | |
i1: Int, | |
i2: Int, | |
) { | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment