Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Como usar dois Forms

From Wiki
Jump to: navigation, search
Article Metadata

Artigo
Tradução:
Por valderind4
Última alteração feita por lpvalente em 15 Mar 2014

Este exemplo mostra como usar dois Forms. Um Form está dentro do MIDlet (SumMIDlet) e o outro é chamado MyResultForm.

No SumMIDlet existe dois campos onde você deve completar com dois valores inteiros, selecionando o comando "calculate" o outro Form aparecerá com a soma destes dois inteiros que você inseriu e se você selecionar o comando "exit" o MIDlet será finalizado.

No MyResultForm existe apenas um campo com o resultado da soma e o comando "back" para retornar ao primeiro form(SumMIDlet).

Código

public class SumMIDlet extends MIDlet implements CommandListener {
 
public static Display d;
private Form mainForm;
private Command okCommand;
private Command exitCommand;
private TextField a;
private TextField b;
 
private MyResultForm resultForm;
 
public SumMIDlet() {
d = Display.getDisplay(this);
this.mainForm = new Form("Equation");
this.okCommand = new Command("Calculate", Command.OK, 0);
this.exitCommand = new Command("Exit", Command.EXIT, 1);
 
this.a = new TextField("A", "", 3, TextField.NUMERIC);
this.b = new TextField("B", "", 4, TextField.NUMERIC);
 
this.resultForm = new MyResultForm("", this.mainForm);
 
this.mainForm.append(this.a);
this.mainForm.append(this.b);
this.mainForm.addCommand(this.exitCommand);
this.mainForm.addCommand(this.okCommand);
this.mainForm.setCommandListener(this);
}
 
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
 
protected void startApp() throws MIDletStateChangeException {
this.d.setCurrent(this.mainForm);
}
 
public void commandAction(Command cmd, Displayable arg1) {
 
if(cmd == this.okCommand) {
int a = Integer.parseInt(this.a.getString());
int b = Integer.parseInt(this.b.getString());
int resultado = a+b;
this.resultForm.setResultado(String.valueOf(resultado));
this.d.setCurrent(this.resultForm);
} else if(cmd == this.exitCommand) {
try {
this.destroyApp(true);
this.notifyDestroyed();
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
 
}
 
}
public class MyResultForm extends Form implements CommandListener {
 
private StringItem resultado;
private Command backCommand;
private Displayable previous;
public MyResultForm(String title, Displayable previousDisplayable) {
super(title);
this.previous = previousDisplayable;
 
this.resultado = new StringItem("Result: ", "");
this.backCommand = new Command("Back", Command.BACK, 1);
 
this.append(this.resultado);
this.addCommand(this.backCommand);
this.setCommandListener(this);
}
 
protected void setResultado(String resultado) {
this.resultado.setText(resultado);
}
 
public void commandAction(Command command, Displayable arg1) {
if(command == this.backCommand)
SumMIDlet.d.setCurrent(this.previous);
}
}

Imagens

Primeiro form (SumMIDlet)

Imagem1.png

Imagem do MyResultForm

Imagem2.png

This page was last modified on 15 March 2014, at 12:34.
168 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×