Olá, fiz um código teste para fazer algo semelhante a uma máscara no formato de preço (123.234,00), no WTK quando eu digito (na ordem) 1.123. ele apaga tudo, porque eu não posso colocar dois pontos, quando eu vou começar a digitar denovo ele funciona normalmente, no PalmSimulator dá um erro e diz que foi um erro de StringIndexOutOfBoundsException… alguém poderia me ajudar…
o código está aí
/*
* TestCustomItem.java
*
* Created on 28 de Dezembro de 2004, 08:52
*/
/**
*
* @author Ricardo
*/
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.ItemStateListener;
public class TesteCustomItem extends MIDlet implements ItemStateListener{
private Display display;
private Form fmMain;
private TextField txfMain;
private TextField txfFone;
private int backup = 0;
// private TextFieldPing txfPing;
/** Creates a new instance of TestCustomItem */
public TesteCustomItem() {
display = Display.getDisplay(this);
fmMain = new Form("Teste Custom Item");
txfMain = new TextField("Nome", "", 15, TextField.ANY);
txfFone = new TextField("Fone", "", 10, TextField.ANY);
fmMain.append(txfMain);
fmMain.append(txfFone);
fmMain.setItemStateListener(this);
// txfPing = new TextFieldPing(null, fmMain.getWidth(), "Custom Item");
// fmMain.append(txfPing);
}
protected void destroyApp(boolean param) {
}
protected void pauseApp() {
}
protected void startApp() {
display.setCurrent(fmMain);
}
public void itemStateChanged(javax.microedition.lcdui.Item item) {
if (item == txfFone)
{
verificaTxf();
}
}
public void verificaTxf()
{
String txf = txfFone.getString();
if (txf.length()>0)
{
int ponto = txf.indexOf('.');
int virgula = txf.indexOf(',');
if (txf.substring(ponto+1).indexOf('.')!=-1)
txfFone.setString("");
else if (ponto == 0 || virgula == 0)
txfFone.setString("");
else if (txf.length()>3 && ponto == -1 && virgula == -1)
txfFone.setString("");
else if (virgula != -1 && ponto != -1 && (virgula - ponto)!= 4)
txfFone.setString("");
else if ((txf.length() - ponto > 4 && virgula == -1) || (txf.length() - virgula > 3 && virgula != -1))
txfFone.setString("");
}
}
}