Created
November 7, 2018 16:28
-
-
Save jesselima/343296e86f83dfa7cecfb155f6c42b3f to your computer and use it in GitHub Desktop.
Mascara para formatação de campos de formulários.
This file contains hidden or 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 com.example.myapplicationutils; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.widget.EditText; | |
public abstract class MaskEditUtil { | |
public static final String FORMAT_CPF = "###.###.###-##"; | |
public static final String FORMAT_FONE = "(###)####-#####"; | |
public static final String FORMAT_CEP = "#####-###"; | |
public static final String FORMAT_DATE = "##/##/####"; | |
public static final String FORMAT_HOUR = "##:##"; | |
public static final String FORMAT_WEIGHT = "###"; | |
public static final String FORMAT_HEIGHT = "#.##"; | |
/** | |
* Método que deve ser chamado para realizar a formatação | |
* | |
* @param ediTxt | |
* @param mask | |
* @return | |
*/ | |
public static TextWatcher mask(final EditText ediTxt, final String mask) { | |
return new TextWatcher() { | |
boolean isUpdating; | |
String old = ""; | |
@Override | |
public void afterTextChanged(final Editable s) {} | |
@Override | |
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {} | |
@Override | |
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { | |
final String str = MaskEditUtil.unmask(s.toString()); | |
String mascara = ""; | |
if (isUpdating) { | |
old = str; | |
isUpdating = false; | |
return; | |
} | |
int i = 0; | |
for (final char m : mask.toCharArray()) { | |
if (m != '#' && str.length() > old.length()) { | |
mascara += m; | |
continue; | |
} | |
try { | |
mascara += str.charAt(i); | |
} catch (final Exception e) { | |
break; | |
} | |
i++; | |
} | |
isUpdating = true; | |
ediTxt.setText(mascara); | |
ediTxt.setSelection(mascara.length()); | |
} | |
}; | |
} | |
public static String unmask(final String s) { | |
return s.replaceAll("[.]", "") | |
.replaceAll("[-]", "") | |
.replaceAll("[/]", "") | |
.replaceAll("[(]", "") | |
.replaceAll("[ ]","") | |
.replaceAll("[:]", "") | |
.replaceAll("[)]", ""); | |
} | |
} | |
/* | |
Após colocar a classe no pacote utils da sua aplicação, ajuste o pacote para que não fique com erro | |
e use nos EditText da seguinte forma: | |
editTextCpf.addTextChangedListener(MaskEditUtil.mask(editTextCpf, MaskEditUtil.FORMAT_CPF)); | |
Ou pode passar a formatação, como segue o exemplo: | |
editTextCpf.addTextChangedListener(MaskEditUtil.mask(editTextCpf, "###.###.###-##")); | |
Formatar os EditText pode auxiliar o usuário da sua aplicação a informar os dados corretamente e | |
evitar erro de digitação além de agilizar o processo de inclusão de dados. | |
* */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment