Textwatcher

51 views
Skip to first unread message

Gabriel Tomé

unread,
Aug 8, 2011, 3:24:07 PM8/8/11
to Android Brasil - Dev
Estou tentando implementar uma máscara automática.
O método onTextChanged funciona tranquilo com outras funcionalidades.
Quando coloco pra formatar não dá certo.
Alguém pode ajudar?
Valeu

txtValor.addTextChangedListener(new TextWatcher() {

public void beforeTextChanged(CharSequence s, int start,
int count, int after) {

}

public void onTextChanged(CharSequence s, int start, int
before, int count) {
NumberFormat formatter = new
DecimalFormat("$#.###.###,##");

txtValor.setText(formatter.format(txtValor.getText()));
}

@Override
public void afterTextChanged(Editable arg0) {
}
});

Diego Rocha

unread,
Aug 8, 2011, 3:28:53 PM8/8/11
to androidb...@googlegroups.com
Gabriel,

Você está entrando em loop infinito. 

  1. O usuário altera o textView.
  2. Seu evento é chamado.
  3. Você altera o valor do textView dentro do seu evento.
  4. Seu evento é novamente chamado.
  5. Sem parar...

Coloca um Flag no seu onTextChanged para evitar o loop infinito.

Atenciosamente,
Diego Rocha

luciofm

unread,
Aug 8, 2011, 3:31:02 PM8/8/11
to androidb...@googlegroups.com
Gabriel, da uma olhada nessa thread


Implementei um edittext com mascara de telefone, tipo (11) 1111-1111

Lúcio Maciel
luc...@gmail.com


2011/8/8 Gabriel Tomé <gabrielt...@hotmail.com>
Reply all
Reply to author
Forward
0 new messages