Java Tutorial/J2ME/List
Содержание
Implicit Menu
<source lang="java">
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; public class J2MEListImplicit extends MIDlet implements CommandListener {
private Display display; private List list; private Command exit; Alert alert; public J2MEListImplicit() { display = Display.getDisplay(this); exit = new Command("Exit", Command.EXIT, 1); list = new List("Menu:", List.IMPLICIT); list.append("New", null); list.append("Open", null); list.addCommand(exit); list.setCommandListener(this); } public void startApp() { display.setCurrent(list); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command == List.SELECT_COMMAND) { String selection = list.getString(list.getSelectedIndex()); alert = new Alert("Option Selected", selection, null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); display.setCurrent(alert); } else if (command == exit) { destroyApp(false); notifyDestroyed(); } }
}</source>
List check box
<source lang="java">
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; public class J2MEListCheckBox extends MIDlet implements CommandListener {
private Display display; private Command exit; private Command submit; private List list; public J2MEListCheckBox() { display = Display.getDisplay(this); list = new List("Select Media", List.MULTIPLE); list.append("Books", null); list.append("Movies", null); list.append("Television", null); list.append("Radio", null); exit = new Command("Exit", Command.EXIT, 1); submit = new Command("Submit", Command.SCREEN, 2); list.addCommand(exit); list.addCommand(submit); list.setCommandListener(this); } public void startApp() { display.setCurrent(list); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable Displayable) { if (command == submit) { boolean choice[] = new boolean[list.size()]; StringBuffer message = new StringBuffer(); list.getSelectedFlags(choice); for (int x = 0; x < choice.length; x++) { if (choice[x]) { message.append(list.getString(x)); message.append(" "); } } Alert alert = new Alert("Choice", message.toString(), null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); display.setCurrent(alert); list.removeCommand(submit); } else if (command == exit) { destroyApp(false); notifyDestroyed(); } }
}</source>
List radio button
<source lang="java">
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; public class J2MEListRadioButtons extends MIDlet implements CommandListener {
private Display display; private Command exit; private Command submit; private List list; public J2MEListRadioButtons() { display = Display.getDisplay(this); list = new List("Select one", List.EXCLUSIVE); list.append("Male", null); list.append("Female", null); exit = new Command("Exit", Command.EXIT, 1); submit = new Command("Submit", Command.SCREEN, 2); list.addCommand(exit); list.addCommand(submit); list.setCommandListener(this); } public void startApp() { display.setCurrent(list); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable Displayable) { if (command == submit) { Alert alert = new Alert("Choice", list.getString(list.getSelectedIndex()), null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); display.setCurrent(alert); list.removeCommand(submit); } else if (command == exit) { destroyApp(false); notifyDestroyed(); } }
}</source>
List with strings and images
<source lang="java">
import java.io.IOException; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.List; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; public class ListMIDlet extends MIDlet implements CommandListener {
TextBox main; private Command exitCommand; private Command exclusiveCommand; private Command multipleCommand; private Command implicitCommand; private Command confirmCommand; private List aEXCLUSIVEList; private List aMULTIPLEList; private List aIMPLICITList; String[] position = { "1", "2", "3" }; String[] hobby = { "A", "B", "C", "D", "E" }; Image[] positionIcon = { createImage("/1.png"), createImage("/2.png"), createImage("/3.png") }; Image[] hobbyIcon = { createImage("/A.png"), createImage("/B.png"), createImage("/C.png"), createImage("/D.png"), createImage("/E.png") }; private Display display; private String currentScreen = ""; public ListMIDlet() { display = Display.getDisplay(this); exitCommand = new Command("exit", Command.SCREEN, 1); exclusiveCommand = new Command("exclusive", Command.SCREEN, 1); multipleCommand = new Command("multiple", Command.SCREEN, 1); implicitCommand = new Command("implicit", Command.SCREEN, 1); confirmCommand = new Command("confirm", Command.SCREEN, 2); } public void startApp() { main = new TextBox("main", null, 256, TextField.ANY); main.addCommand(exitCommand); main.addCommand(exclusiveCommand); main.addCommand(multipleCommand); main.addCommand(implicitCommand); main.setCommandListener(this); display.setCurrent(main); currentScreen = "Main"; } public void pauseApp() { } public void destroyApp(boolean unconditional) { } private Image createImage(String name) { Image aImage = null; try { aImage = Image.createImage(name); } catch (IOException e) { } return aImage; } private void exclusiveList() { aEXCLUSIVEList = new List("exclusive", List.EXCLUSIVE, position, positionIcon); aEXCLUSIVEList.addCommand(exitCommand); aEXCLUSIVEList.addCommand(confirmCommand); aEXCLUSIVEList.setCommandListener(this); display.setCurrent(aEXCLUSIVEList); currentScreen = "exclusive"; } private void multipleList() { aMULTIPLEList = new List("multiple", List.MULTIPLE, hobby, hobbyIcon); aMULTIPLEList.addCommand(exitCommand); aMULTIPLEList.addCommand(confirmCommand); aMULTIPLEList.setCommandListener(this); display.setCurrent(aMULTIPLEList); currentScreen = "multiple"; } private void implicitList() { aIMPLICITList = new List("implicit", List.IMPLICIT); aIMPLICITList.append("A", createImage("/A.png")); aIMPLICITList.append("B", createImage("/B.png")); aIMPLICITList.append("C", createImage("/C.png")); aIMPLICITList.append("D", createImage("/D.png")); aIMPLICITList.append("E", createImage("/E.png")); aIMPLICITList.addCommand(exitCommand); aIMPLICITList.setCommandListener(this); display.setCurrent(aIMPLICITList); currentScreen = "implicit"; } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { if (currentScreen == "implicit") { destroyApp(false); notifyDestroyed(); } else { display.setCurrent(main); currentScreen = "no"; } } if (c == exclusiveCommand) { exclusiveList(); } if (c == multipleCommand) { multipleList(); } if (c == implicitCommand) { implicitList(); } if (c == confirmCommand) { Alert aAlert; if (currentScreen == "implicit") { int aIndex = aEXCLUSIVEList.getSelectedIndex(); aAlert = new Alert("Result", position[aIndex], null, AlertType.INFO); display.setCurrent(aAlert); } if (currentScreen == "multiple") { String result = "result\n"; for (int i = 0; i < aMULTIPLEList.size(); i++) if (aMULTIPLEList.isSelected(i)) result += hobby[i] + ", "; aAlert = new Alert("alert", result, null, AlertType.INFO); aAlert.setTimeout(5000); display.setCurrent(aAlert); } } if (c == List.SELECT_COMMAND) { int aIndex = aIMPLICITList.getSelectedIndex(); String menuItem = aIMPLICITList.getString(aIndex); Alert aAlert = new Alert("imple", menuItem, null, AlertType.INFO); aAlert.setTimeout(5000); display.setCurrent(aAlert); } }
}</source>
Use List
<source lang="java">
import java.io.IOException; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.rumand; import javax.microedition.lcdui.rumandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; public class J2METravelList extends MIDlet implements CommandListener {
private List mList; private Command mExitCommand, mNextCommand; public J2METravelList() { String[] stringElements = { "A", "C", "H" }; Image[] imageElements = { loadImage("/a.png"), loadImage("/c.png"), loadImage("/h.png") }; mList = new List("Reservation type", List.IMPLICIT, stringElements, imageElements); mNextCommand = new Command("Next", Command.SCREEN, 0); mExitCommand = new Command("Exit", Command.EXIT, 0); mList.addCommand(mNextCommand); mList.addCommand(mExitCommand); mList.setCommandListener(this); } public void startApp() { Display.getDisplay(this).setCurrent(mList); } public void commandAction(Command c, Displayable s) { if (c == mNextCommand || c == List.SELECT_COMMAND) { int index = mList.getSelectedIndex(); Alert alert = new Alert("Your selection", "You chose " + mList.getString(index) + ".", null, AlertType.INFO); Display.getDisplay(this).setCurrent(alert, mList); } else if (c == mExitCommand) notifyDestroyed(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } private Image loadImage(String name) { Image image = null; try { image = Image.createImage(name); } catch (IOException ioe) { System.out.println(ioe); } return image; }
}</source>
Use List option to hold choice
<source lang="java">
import javax.microedition.lcdui.Choice; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; public class PaymentMIDlet extends MIDlet {
private Display display; List options = new List("Method of Payment", Choice.EXCLUSIVE); public void startApp() { display = Display.getDisplay(this); options.append("Visa", null); options.append("MasterCard", null); options.append("Amex", null); display.setCurrent(options); } public void pauseApp() { } public void destroyApp(boolean unconditional) { }
}</source>