I'm trying to automatically set a mask on the field, I do not know any other way, I'm doing this:
if(s.equals('(')){
edtCelular.setText(texto);
}else{
texto = "(" + telefone.substring(0, 2) + ") " + telefone.substring(2, 7) + "-" + telefone.substring(7, 11);
edtCelular.setText(texto);
}
And it's not working, is there any way to do this?
It is being set when the field focus is coming out. As an indication of a response I did the upgrade but still give some different problems.
I have class MASK
that looks like this:
package com.fomedemais.FomeDemais;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class Mask {
public static String unmask(String s) {
return s.replaceAll("[.]", "").replaceAll("[-]", "")
.replaceAll("[/]", "").replaceAll("[(]", "")
.replaceAll("[)]", "");
}
public static TextWatcher insert(final String mask, final EditText ediTxt) {
return new TextWatcher() {
boolean isUpdating;
String old = "";
public void onTextChanged(CharSequence s, int start, int before,int count) {
String str = Mask.unmask(s.toString());
String mascara = "";
if (isUpdating) {
old = str;
isUpdating = false;
return;
}
int i = 0;
for (char m : mask.toCharArray()) {
if (m != '#' && str.length() > old.length()) {
mascara += m;
continue;
}
try {
mascara += str.charAt(i);
} catch (Exception e) {
break;
}
i++;
}
isUpdating = true;
ediTxt.setText(mascara);
ediTxt.setSelection(mascara.length());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
};
}
}
And when I do the event I'm doing this:
@FocusChange(R.id.edtTelefone)
void focusAlterado(View v, boolean hasFocus) {
if(!hasFocus){
edtTelefone.addTextChangedListener(Mask.insert("(##)####-####", edtTelefone));
}
}
LOG ERROR