解读 LWUIT 之八:模态对话框(Dialog)
2010-03-01 15:11
369 查看
解读 LWUIT 之八:模态对话框(Dialog)
LWUIT 开发指南下载
作者写的 Hello Dialog 源代码下载
Dialog 是一个 Form,它会占据屏幕最上层。Dialog 默认作为一个模态实体展示给用户,模态表明 Dialog 将会阻塞调用它的那个线程,即使这个线程可能是事件派发线程(EDT:Event Dispatcher Thread)。通过 Dialog 我们可以快速给用户提示信息,并将 Dialog 的 show 方法结束后返回的信息作为用户的回复。每个 Dialog 都有一个位于 Dialog 中心位置的 Dialog 体,Dialog 体内可以加入一个 LWUIT 控件,因此我们可以在
Dialog 中使用自定义控件或者预构建容器。
注意模态 Dialog 在 dispose 方法调用之前会一直阻塞调用它的线程,比如我们显式地调用另一个 Form 的 show 方法并不会奏效,因为这样做并不释放该线程的阻塞。
为了增强用户体验,LWUIT 提供了五种类型的 Dialog:ALARM、CONFIRMATION、ERROR、INFO、WARNING。我们可以通过调用 Tree 的 setDialogType 方法来设置 Dialog 类型,默认的类型会打开手机设备的警告声。
那么怎么创建并显示一个 Dialog 呢?两种办法:一、可以直接调用 Dialog 的静态 show 方法创建并显示 Dialog;二、也可以使用 Dialog 的构造方法创建,然后调用创建实例的 show 方法显示。(很多朋友倾向于使用第一种办法,省事,呵呵)。使用静态的 show 方法我们可以布局标准 Dialog、设置 Dialog 图标、定义 Dialog 的主题和文本内容、定义 Dialog 按钮文本内容。尽管每个 show 方法的参数数目不一,但是这些参数却遵循一定的标准。下面是这些
show 方法参数的具体说明。
String title:Dialog 的标题;
Component body:被放置在 Dialog 中间位置的控件。当然这个控件可以作为一个容器来放置其他控件;
String text:可以取代 Dialog 体显示在 Dialog 中间位置的文本;
Command[] cmds:一组添加到 Dialog 的 Command 控件,任何一个 Command 被点击都会销毁 Dialog。确认和取消是最常用的 Command;
int type:定义 Dialog 是 TYPE_WARNING、TYPE_INFO、TYPE_ERROR、TYPE_CONFIRMATION 或者 TYPE_ALARM 中的一种类型;
Image icon:Dialog 中显示的图标;
long timeout:定义 Dialog 超时时间,单位是毫秒,这个时间过后 Dialog 会自动关闭并且返回值为 null。如果 timeout 值被设置为 0,Dialog 会一直保持打开,直到它自己的 dispose 方法被调用;
Transition transition:Dialog 进入屏幕和离开屏幕的过渡方式(更多 Transition 相关信息请留意作者后续博客);
String okText:终止 Dialog 显示的 Command 的显示文本;
String cancelText:取消 Dialog 显示的 Command 文本内容;
int top:设置 Dialog 到屏幕最上方的距离;
int bottom:设置 Dialog 到屏幕下方的距离;
int left:设置 Dialog 到屏幕左方的距离;
int right:设置 Dialog 到屏幕右方的距离;
boolean includeTitle:定义标题显示在屏幕上方还是显示在 Dialog 的内容窗口。
一般来说,show 方法有两种返回类型:Command 和 boolean,Command 型表明返回的是用户点击的 Command 对象,boolean 型返回表明 OK Command 对象被点击。程序通过这种办法来获取用户输入。
Dialog 有两个非静态的 show 方法。无参的那个会创建一个没有任何 Command 的 Dialog。关闭这种 Dialog 的唯一途径就是调用该 Dialog 的 dispose 方法,然而 Dialog 已经将调用它的那个线程阻塞了,我们无法在那个线程里调用。因此我们应该在 Dialog 显示之前先启动一个线程,由该线程负责调用调用 Dialog 的 dispose 方法来关闭。一般的做法是在 Dialog 显示之前启动一个计时器,计时器在 Dialog 显示之前就开始启动;另外那个非静态的
show 方法有 5 个参数,前 4 个设置 Dialog 边缘到屏幕边缘的空白宽度(以像素为单位),最后一个设置 Dialog 内容体中是否包含 Dialog 构造方法中指派的标题。
作者写的 HelloDialog 源代码:
HelloDialog 运行效果图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/20/ba2143bddc9f0a9b0e818b7b5d627fed.jpg)
在上图中,选择 “Ok”,Dialog 关闭,组合框下边的 Label 就显示你选择的内容。否则不显示。
LWUIT 开发指南下载
作者写的 Hello Dialog 源代码下载
Dialog 是一个 Form,它会占据屏幕最上层。Dialog 默认作为一个模态实体展示给用户,模态表明 Dialog 将会阻塞调用它的那个线程,即使这个线程可能是事件派发线程(EDT:Event Dispatcher Thread)。通过 Dialog 我们可以快速给用户提示信息,并将 Dialog 的 show 方法结束后返回的信息作为用户的回复。每个 Dialog 都有一个位于 Dialog 中心位置的 Dialog 体,Dialog 体内可以加入一个 LWUIT 控件,因此我们可以在
Dialog 中使用自定义控件或者预构建容器。
注意模态 Dialog 在 dispose 方法调用之前会一直阻塞调用它的线程,比如我们显式地调用另一个 Form 的 show 方法并不会奏效,因为这样做并不释放该线程的阻塞。
为了增强用户体验,LWUIT 提供了五种类型的 Dialog:ALARM、CONFIRMATION、ERROR、INFO、WARNING。我们可以通过调用 Tree 的 setDialogType 方法来设置 Dialog 类型,默认的类型会打开手机设备的警告声。
那么怎么创建并显示一个 Dialog 呢?两种办法:一、可以直接调用 Dialog 的静态 show 方法创建并显示 Dialog;二、也可以使用 Dialog 的构造方法创建,然后调用创建实例的 show 方法显示。(很多朋友倾向于使用第一种办法,省事,呵呵)。使用静态的 show 方法我们可以布局标准 Dialog、设置 Dialog 图标、定义 Dialog 的主题和文本内容、定义 Dialog 按钮文本内容。尽管每个 show 方法的参数数目不一,但是这些参数却遵循一定的标准。下面是这些
show 方法参数的具体说明。
String title:Dialog 的标题;
Component body:被放置在 Dialog 中间位置的控件。当然这个控件可以作为一个容器来放置其他控件;
String text:可以取代 Dialog 体显示在 Dialog 中间位置的文本;
Command[] cmds:一组添加到 Dialog 的 Command 控件,任何一个 Command 被点击都会销毁 Dialog。确认和取消是最常用的 Command;
int type:定义 Dialog 是 TYPE_WARNING、TYPE_INFO、TYPE_ERROR、TYPE_CONFIRMATION 或者 TYPE_ALARM 中的一种类型;
Image icon:Dialog 中显示的图标;
long timeout:定义 Dialog 超时时间,单位是毫秒,这个时间过后 Dialog 会自动关闭并且返回值为 null。如果 timeout 值被设置为 0,Dialog 会一直保持打开,直到它自己的 dispose 方法被调用;
Transition transition:Dialog 进入屏幕和离开屏幕的过渡方式(更多 Transition 相关信息请留意作者后续博客);
String okText:终止 Dialog 显示的 Command 的显示文本;
String cancelText:取消 Dialog 显示的 Command 文本内容;
int top:设置 Dialog 到屏幕最上方的距离;
int bottom:设置 Dialog 到屏幕下方的距离;
int left:设置 Dialog 到屏幕左方的距离;
int right:设置 Dialog 到屏幕右方的距离;
boolean includeTitle:定义标题显示在屏幕上方还是显示在 Dialog 的内容窗口。
一般来说,show 方法有两种返回类型:Command 和 boolean,Command 型表明返回的是用户点击的 Command 对象,boolean 型返回表明 OK Command 对象被点击。程序通过这种办法来获取用户输入。
Dialog 有两个非静态的 show 方法。无参的那个会创建一个没有任何 Command 的 Dialog。关闭这种 Dialog 的唯一途径就是调用该 Dialog 的 dispose 方法,然而 Dialog 已经将调用它的那个线程阻塞了,我们无法在那个线程里调用。因此我们应该在 Dialog 显示之前先启动一个线程,由该线程负责调用调用 Dialog 的 dispose 方法来关闭。一般的做法是在 Dialog 显示之前启动一个计时器,计时器在 Dialog 显示之前就开始启动;另外那个非静态的
show 方法有 5 个参数,前 4 个设置 Dialog 边缘到屏幕边缘的空白宽度(以像素为单位),最后一个设置 Dialog 内容体中是否包含 Dialog 构造方法中指派的标题。
作者写的 HelloDialog 源代码:
package com.defonds.lwuit; import com.sun.lwuit.CheckBox; import com.sun.lwuit.ComboBox; import com.sun.lwuit.Command; import com.sun.lwuit.Component; import com.sun.lwuit.Dialog; import com.sun.lwuit.Display; import com.sun.lwuit.Form; import com.sun.lwuit.Label; import com.sun.lwuit.List; import com.sun.lwuit.animations.CommonTransitions; import com.sun.lwuit.events.ActionEvent; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.layouts.BoxLayout; import com.sun.lwuit.list.ListCellRenderer; import com.sun.lwuit.plaf.UIManager; import com.sun.lwuit.util.Resources; public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{ private Form exampleContainer;// declare a Form private Label comboBoxLabel;//declare a Label private String[] content = { "Red", "Blue", "Green", "Yellow" }; private ComboBox comboBox;//declare a ComboBox private boolean userInput;//remeber the user input public void startApp() { // init the LWUIT Display Display.init(this); // Setting the application theme is discussed // later in the theme chapter and the resources chapter try { Resources r = Resources.open("/myresources.res"); UIManager.getInstance().setThemeProps(r.getTheme("myresources")); } catch (java.io.IOException e) {} exampleContainer = new Form("Form Title");// Create a Form comboBoxLabel = new Label(" "); comboBox = new ComboBox(content);//Creating the combo box comboBox.setListCellRenderer(new checkBoxRenderer());//Setting a checkBox renderer comboBox.addActionListener(this);//Adding a action listener to catch user clicking exampleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));//Set LayoutManager exampleContainer.addComponent(comboBox);//Add a ComboBox to the Form content pane exampleContainer.addComponent(comboBoxLabel);//Add a Label to the Form content pane exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade exampleContainer.addCommand(new Command("Run", 2));//Add Command key exampleContainer.show();//Show it } public void pauseApp() {} public void destroyApp(boolean unconditional) {} //implements the method of ActionListener public void actionPerformed(ActionEvent evt) { Thread dialogThread = new Thread(){ public void run(){ userInput = Dialog.show("Dialog Title", "You have selected " + content[comboBox.getSelectedIndex()] + ".Are you sure to use it?", "Ok", "Cancel");//create a Dialog if(userInput){ comboBoxLabel.setText(content[comboBox.getSelectedIndex()] + " selected");//change the content of label } } }; dialogThread.start(); } /** * Demonstrates implementation of a renderer derived from a CheckBox */ private static class checkBoxRenderer extends CheckBox implements ListCellRenderer{ /** Creates a new instance of checkBoxRenderer */ public checkBoxRenderer(){ super(""); } // Setting the current check box text and status public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) { setText("" + value); if (isSelected){ setFocus(true); setSelected(true); }else{ setFocus(false); setSelected(false); } return this; } // Returning the list focus component public Component getListFocusComponent(List list) { setText(""); setFocus(true); setSelected(true); return this; } } }
HelloDialog 运行效果图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/20/ba2143bddc9f0a9b0e818b7b5d627fed.jpg)
在上图中,选择 “Ok”,Dialog 关闭,组合框下边的 Label 就显示你选择的内容。否则不显示。
相关文章推荐
- 解读 LWUIT 之八:模态对话框(Dialog)
- IE对话框showModalDialog(模态)和showModelessDialog
- 解读 LWUIT 之四:LWUIT 控件(中)
- 解读 LWUIT 之六:使用表格(Table)和树(Tree)
- 解读 LWUIT 之九:使用布局管理器(Layout Manager)
- jQuery UI 对话框(Dialog) - 模态表单
- 解读 LWUIT 之二:关于 LWUIT 开发指南中的 Hello World
- 解读 LWUIT 之九:使用布局管理器(Layout Manager)
- 解读 LWUIT 之十一:风格(com.sun.lwuit.plaf.Style)对象
- LWUIT 自定义漂亮的Dialog
- 解读 LWUIT 之二:关于 LWUIT 开发指南中的 Hello World
- 解读 LWUIT 之三:LWUIT 控件(上)
- 解读 LWUIT 之十一:风格(com.sun.lwuit.plaf.Style)对象
- 解读 LWUIT 之十三:可插拔主题(Theming)
- 解读 LWUIT 之三:LWUIT 控件(上)
- 解读 LWUIT 之五:LWUIT 控件(下)
- 解读 LWUIT 之十三:可插拔主题(Theming)
- LWUIT 自定义漂亮的Dialog
- 转帖:IE对话框showModalDialog(模态)和showModelessDialog
- LWUIT 自定义漂亮的Dialog