⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialog.java

📁 基于J2ME的手机地图客户端源码
💻 JAVA
字号:
package org.sreid.j2me.util;import java.util.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;/** Convenience class for implementing dialogs. */public class Dialog extends Form implements CommandListener {	private final int type;	private static final int TYPE_TEXT = 1;	private static final int TYPE_CHOICE_EXCLUSIVE = 2;	private static final int TYPE_CHOICE_MULTIPLE = 3;	private static final int TYPE_CONFIRMATION = 4;	private static final int TYPE_GAUGE = 5;	/** User response for confirmation dialog. */	public static final Object USER_CONFIRMED = new Object();	// only one of these will be set, depending on type	private TextField textField = null;	private ChoiceGroup choiceGroup = null;	private Gauge gauge = null;	private Runnable callback;	private Object userResponse;	private MIDlet midlet;	private Displayable nextDisplayable;	private Dialog(String title, int type, boolean addOkCancel) {		super(title);		this.type = type;		setCommandListener(this);		if (addOkCancel) {			addCommand(new Command("OK", Command.OK, 1));			addCommand(new Command("Cancel", Command.CANCEL, 2));		}	}	/** Constructs a Dialog with a text field. The resulting userResponse will be a String, or null. */	public static Dialog createTextDialog(String title, String label, String defaultText, int maxSize, int constraints) {		Dialog dlg = new Dialog(title, TYPE_TEXT, true);		dlg.textField = new TextField(label, defaultText, maxSize, constraints);		dlg.append(dlg.textField);		return dlg;	}	/** Constructs a Dialog with a text field. The resulting userResponse will be a String, or null. */	public static Dialog createTextDialog(String label, String defaultText) {		return createTextDialog(label, label, defaultText, 200, TextField.ANY);	}	/** Constructs a Dialog with several options for the user to choose from. The resulting userResponse will be a String, or null. */	public static Dialog createExclusiveChoiceDialog(String title, String label, String[] options) {		return createChoiceDialog(title, TYPE_CHOICE_EXCLUSIVE, label, options);	}	/** Constructs a Dialog with several options for the user to choose from. The resulting userResponse will be an array of Strings, or null. */	public static Dialog createMultipleChoiceDialog(String title, String label, String[] options) {		return createChoiceDialog(title, TYPE_CHOICE_MULTIPLE, label, options);	}	private static Dialog createChoiceDialog(String title, int type, String label, String[] options) {		Dialog dlg = new Dialog(title, type, true);		dlg.choiceGroup = new ChoiceGroup(label, (type == TYPE_CHOICE_EXCLUSIVE ? Choice.EXCLUSIVE : Choice.MULTIPLE), options, null);		dlg.append(dlg.choiceGroup);		return dlg;	}	/** Constructs a Dialog with exactly two choices. The resulting userResponse will be USER_CONFIRMED, or null. */	public static Dialog createConfirmationDialog(String title, String label, String okOption, String cancelOption) {		Dialog dlg = new Dialog(title, TYPE_CONFIRMATION, false);		dlg.append(new StringItem("", label));		dlg.addCommand(new Command(okOption, Command.OK, 1));		dlg.addCommand(new Command(cancelOption, Command.CANCEL, 2));		return dlg;	}	/** Constructs a Dialog with a slider. The resulting userResponse will be an Integer, or null. */	public static Dialog createGaugeDialog(String title, String label, int maxValue, int initialValue) {		Dialog dlg = new Dialog(title, TYPE_GAUGE, true);		dlg.gauge = new Gauge(label, true, maxValue, initialValue);		dlg.append(dlg.gauge);		return dlg;	}	/** Shows this dialog on the specified midlet, then returns to the current screen. */	public void showOn(MIDlet midlet) {		showOn(midlet, Display.getDisplay(midlet).getCurrent());	}	/** Shows this dialog on the specified midlet, then shows nextDisplayable when this dialog is dismissed. */	public void showOn(MIDlet midlet, Displayable nextDisplayable) {		if (midlet == null) throw new IllegalArgumentException("midlet argument needs to be non-null");		if (nextDisplayable == null) throw new IllegalArgumentException("nextDisplayble argument needs to be non-null");		this.midlet = midlet;		this.nextDisplayable = nextDisplayable;		Display.getDisplay(midlet).setCurrent(this);	}	/** Calls showOn, but enqueues the call using Display.showSerially. */	public void showSerially(final MIDlet midlet, final Displayable nextDisplayable) {		Display.getDisplay(midlet).callSerially(new Runnable() { public void run() {			showOn(midlet, nextDisplayable);		}});	}	/** Returns the user's response. */	public Object getUserResponse() {		return userResponse;	}	/** Sets the callback that will be called when the user has given their response, or cancelled the dialog. */	public void setCallback(Runnable callback) {		this.callback = callback;	}	public void commandAction(Command c, Displayable d) {		if (c.getCommandType() == Command.OK) {			switch (type) {				case TYPE_TEXT: {					userResponse = textField.getString();					break;				}				case TYPE_CHOICE_EXCLUSIVE: {					userResponse = choiceGroup.getString(choiceGroup.getSelectedIndex());					break;				}				case TYPE_CHOICE_MULTIPLE: {					int size = choiceGroup.size();					boolean[] flags = new boolean[size];					int selectionCount = choiceGroup.getSelectedFlags(flags);					String[] selections = new String[selectionCount];					int idx = 0;					for (int i = 0; i < size; i++) {						if (flags[i]) selections[idx++] = choiceGroup.getString(i);					}					userResponse = selections;					break;				}				case TYPE_GAUGE: {					userResponse = new Integer(gauge.getValue());					break;				}				case TYPE_CONFIRMATION: {					userResponse = USER_CONFIRMED;					break;				}				default: {					throw new IllegalStateException("Unknown type: " + type);				}			}		}		else if (c.getCommandType() == Command.CANCEL) {			userResponse = null;		}		else {			throw new IllegalStateException("Unrecognized command type: " + c);		}		if (callback != null) callback.run();		Display.getDisplay(midlet).setCurrent(nextDisplayable);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -