Olá pessoal, criei uma classe para fazer o mapeamento de um edit text para o campo de telefone no seguinte formato
(##)####-####
a princípio está dando certo, porém hora que digito o último digito
ele da esse erro
ArrayIndexOutOfBounds
segue o código
package br.com.tudaki.services;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
/**
* Classe para mapear o campo de telefone do cadastro
* @author Jilles Ragonha
*(##)####-#### - formato
*/
public class EditTextTelefoneMascarado extends EditText {
private boolean isUpdating;
/**
* Mapeando a posição do cursor do telefone para a mascara 1234567890 =>
* (12) 3456-7890
*/
private int posicionando[] = { 1, 2, 3, 6, 7, 8, 9, 11, 12, 13};
/**
* construtores
*
* @param context
* @param attrs
* @param defStyle
*/
public EditTextTelefoneMascarado(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
inicializando();
}
public EditTextTelefoneMascarado(Context context, AttributeSet attrs) {
super(context, attrs);
inicializando();
}
public EditTextTelefoneMascarado(Context context) {
super(context);
inicializando();
}
public String getCleanText() {
String text = EditTextTelefoneMascarado.this.getText().toString();
text.replaceAll("[^0-9]*", "");
return text;
}
private void inicializando() {
final int tamanhoMaximo = 10;
this.setKeyListener(keyListenerNumber);
this.setText("( ) - ");
this.setSelection(1);
this.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
String atual = s.toString();
Log.i("atual", atual);
//evita loop infinito
if (isUpdating) {
isUpdating = false;
return;
}
// Retirar tudo que não for número do texto
String numero = atual.replaceAll("[^0-9]*","");
if (numero.length() > 10) {
numero = numero.substring(0, 11);
}
int tamanho = numero.length();
// Deixando o numero com tamanho 10
String numeroFormatado = padNumero(numero, tamanhoMaximo);
// Separando o telefone em parter
String ddd = numeroFormatado.substring(0, 2);
String parte1 = numeroFormatado.substring(2, 6);
String parte2 = numeroFormatado.substring(6, 10);
//criar a mascara para o telefone
String telefone = "("+ ddd + ")"+parte1+"-"+parte2;
isUpdating = true;
EditTextTelefoneMascarado.this.setText(telefone);
EditTextTelefoneMascarado.this.setSelection(posicionando[tamanho]); // NESSA LINHA OCORRE O ERRO
}
});
}
protected String padNumero(String numero, int tamanhoMaximo){
String padded = new String(numero);
for(int i =0; i<tamanhoMaximo-numero.length();i++)
{
padded +=" ";
}
return padded;
}
private final KeyListenerNumber keyListenerNumber = new KeyListenerNumber();
}
Olha que estranho o que está acontecendo no meu LogCat
essa é a saída dele
06-07 07:49:38.381: I/atual(30790): (3 ) -
06-07 07:49:38.381: I/atual(30790): (3 ) -
06-07 07:49:38.711: I/atual(30790): (35 ) -
06-07 07:49:38.721: I/atual(30790): (35) -
06-07 07:49:38.972: I/atual(30790): (356) -
06-07 07:49:38.981: I/atual(30790): (35)6 -
06-07 07:49:39.251: I/atual(30790): (35)6 6 -
06-07 07:49:39.261: I/atual(30790): (35)66 -
06-07 07:49:39.591: I/atual(30790): (35)66 7 -
06-07 07:49:39.601: I/atual(30790): (35)667 -
06-07 07:49:39.912: I/atual(30790): (35)667 8-
06-07 07:49:39.921: I/atual(30790): (35)6678-
06-07 07:49:40.191: I/atual(30790): (35)6678-8
06-07 07:49:40.191: I/atual(30790): (35)6678-8
06-07 07:49:40.451: I/atual(30790): (35)6678-8 8
06-07 07:49:40.451: I/atual(30790): (35)6678-88
06-07 07:49:40.681: I/atual(30790): (35)6678-88 8
06-07 07:49:40.691: I/atual(30790): (35)6678-888
06-07 07:49:41.211: I/atual(30790): (35)6678-888 9
06-07 07:49:41.221: I/atual(30790): (35)6678-8889