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.

Revision as of 19:50, 16 April 2014 by lpvalente (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Como criar uma janela de confirmação em Java ME

From Wiki
Jump to: navigation, search
Article Metadata

Artigo
Tradução:
Por maiconherverton
Última alteração feita por lpvalente em 16 Apr 2014

Este exemplo mostra como criar uma janela de confirmação em Java ME usando a classe padrão da LCDUI: Alert. Essa janela poderia ser usada, por exemplo, para confirmar o fechamento de um MIDlet.

O código fonte completo para um MIDlet de teste:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class ConfirmationMIDlet extends MIDlet implements CommandListener {
private Form form;
private Alert alert;
private Command exitCommand;
 
public void startApp() {
form = new Form("ConfirmationMIDlet");
exitCommand = new Command("Sair", Command.EXIT, 1);
form.setCommandListener(this);
form.addCommand(exitCommand);
Display.getDisplay(this).setCurrent(form);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
public void commandAction (Command c, Displayable d) {
if (c == exitCommand) {
showConfirmation("Confirmação", "Você tem certeza que quer sair?");
}
}
 
private void closeAlert() {
Display.getDisplay(this).setCurrent(form);
alert = null;
}
 
protected void showConfirmation(String title, String text) {
alert = new Alert(title, text, null, AlertType.CONFIRMATION);
alert.addCommand(new Command ("Yes", Command.OK, 1));
alert.addCommand(new Command("No", Command.CANCEL, 1));
alert.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c.getLabel().equals("Yes")) {
notifyDestroyed();
}
if (c.getLabel().equals("No")) {
closeAlert();
}
}
});
Display.getDisplay(this).setCurrent(alert, form);
}
}
This page was last modified on 16 April 2014, at 19:50.
183 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.

×