[Concluído]Ajuda - Mapeando um Edit Text no android - Erro ArrayIndexOutOfBounds

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 :frowning:
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

Olá
O array varia de 0 a n-1
então acerta essa linha: EditTextTelefoneMascarado.this.setSelection(posicionando[tamanho]); // NESSA LINHA OCORRE O ERRO

coloca tamanho-1

eu fiz isso
e realmente não dá mais erro
mas olha o que houve quando eu digito no campo
1234567890
deveria ficar assim
(12)3456-7890
mas esta ficando assim
(23)4156-8790

segue a saída do log cat


06-07 11:52:39.531: I/atual(381): (1 ) -
06-07 11:52:39.571: I/atual(381): (1 ) -
06-07 11:52:40.501: I/atual(381): (21 ) -
06-07 11:52:40.591: I/atual(381): (21) -
06-07 11:52:41.541: I/atual(381): (231) -
06-07 11:52:41.561: I/atual(381): (23)1 -
06-07 11:52:43.382: I/atual(381): (234)1 -
06-07 11:52:43.422: I/atual(381): (23)41 -
06-07 11:52:44.272: I/atual(381): (23)415 -
06-07 11:52:44.291: I/atual(381): (23)415 -
06-07 11:52:45.342: I/atual(381): (23)4156 -
06-07 11:52:45.372: I/atual(381): (23)4156-
06-07 11:52:46.562: I/atual(381): (23)41567-
06-07 11:52:46.602: I/atual(381): (23)4156-7
06-07 11:52:47.452: I/atual(381): (23)4156-87
06-07 11:52:47.472: I/atual(381): (23)4156-87
06-07 11:52:48.272: I/atual(381): (23)4156-879
06-07 11:52:48.312: I/atual(381): (23)4156-879
06-07 11:52:49.522: I/atual(381): (23)4156-8790
06-07 11:52:49.552: I/atual(381): (23)4156-8790

Você deve estar usando o indice do array sem subtrair 1
Checa toda manipulação de array para considerar isso.

Na verdade voce tem que incluir mais um item no array posicionando e deixar a linha alterada como estava antes.

acho que tenho quase certeza que nao entendi hehe
incluir mais um item no array? porquê?

Porque o cursor deve sempre ficar na frente do ultimo digito digitado.
Quando digitar o ultimo, ele deveria ficar na frente, como você definiu o array com 10 posições ele ultrapassa o tamanho do array
Veja que esse ultimo item não vai ser usado, é só para facilitar a lógica

ahhhh agora sim
hehe
faz todo sentido
vou tentar… brigadao msm

kara funcionou perfeitamente
obrigado mesmo!!!