您的位置:首页 > 其它

在编写手机程序时,经常要进行各个屏幕间的切换

2008-10-20 21:30 288 查看
在编写手机程序时,经常要进行各个屏幕间的切换
功能:在主程序中有多个按钮,每一个按钮对应一个功能,每一个功能要不同的屏幕(元素)表现出来。
实现:
一、主程序中必然定义了一个display对像,如private Display display,它表示当前的屏幕。还有一些Displayable对像。如form,textfield等都是displayable的子类。在主程序中通过dipslay.sercurrent(displayable实例名); 即可将当displayable实例加入当前的屏幕。以下程序:

private Display display=Display.getDisplay(this);
private Form form = new Form(“New Form“);
public void startapp()
{
display.setcurrent(form);
}

作用是将form添加到当前的屏幕当中。

二、要想进行屏幕间的切换,只要将你想显示的东东放到到主程序的display对象中即可。主程序中定义了一个display,则要在另一个屏幕(我姑且把它称之为目标屏幕)中引用到主程序的display。

用以下代码说明:

mainmidlet.java:主程序,一个标准的midlet。

import javax.microedition.midlet.midlet;
import javax.microedition.lcdui.*;

public class mainmidlet extends midlet implements commandlistener {

private display display;
private form form = new form("wellcome!!");
private command okcommand = new command("ok",command.ok,1);
//选择ok,换到下一个屏幕
private form ns ;
private stringitem si = new stringitem("first screen","~_~");
public mainmidlet()
{
form.addcommand(okcommand);
form.append(si);
}
public void startapp() {
display = display.getdisplay(this);

display.setcurrent(form);
form.setcommandlistener(this);//对form加入commandlistener

}
public void pauseapp() {

}
public void destroyapp(boolean b){

}
public void commandaction(command c,displayable s)
{
if(c==okcommand)
{
ns = new nextscreen(display,form);//最关键的地方在这里:form:将父屏幕对象传下去,方便后退时返回父屏幕)
display.setcurrent(ns);
}

}

}

在这个midlet中,定义了一个display对像display。
以及一个两个displayable对象form form及stringitem si。运行后显示在屏幕当中。
还有个一command okcommand,其作用是触发下一个屏幕。
在public void commandlistener中可以看到,当当前按下的按钮是okcommand时,初始化一个nextscreen对象
ns = new nextscreen(display,form);
将display和form传入,作用就是进行屏幕的切换。
下面是nextscreen的实现:
nextscreen.java 第二个屏幕的代码

import javax.microedition.lcdui.*;
public class nextscreen extends form implements commandlistener {

private display display;
private displayable parent;
private command backcommand = new command("back",command.back,1);
private stringitem si = new stringitem("secondscrean","~_~");
public nextscreen(display d,displayable p)
{
super("nextscreen");
display = d;
parent = p;
append(si);
addcommand(backcommand);
setcommandlistener(this);

}
public void commandaction(command c,displayable s)
{
//返回上一个屏幕
if(c==backcommand)
{
display.setcurrent(parent);
}
}

}

它继承自form类。
在nextscreen中又定义了一个display display,将用它来标识当前的元素显示在哪一个屏幕中。
一个form form,一个stringitem si。这是当前屏幕中要显示的东东:)
构造函数中的super("secondscreen");的作用是使得nextscreen可以直接调中其父类form中的函数。
backcommand的作用是返回上一个屏幕。
将form,si及backcommand加入nextscreen中,一个nextscreen的实例就完成了。在主程序(mainmidlet)中,就是ns。

接下来,最关键的地方,就是在mainmidlet中的这一句:display.setcurrent(ns);
就是把ns在当前的屏幕中显示出来!这样就可以看到nextscreen中定义的各个元素(form,si)了!

然后想返回原屏幕,怎么办呢?这时nextscreen中的backcommand就起作用了。
仔细看这两句:
在mainmidlet.java中:
ns = new nextscreen(display,form);
它将form也传了进去。它有什么用呢?
在nextscreen的构造函数中:
dispaly =d;
这一句其实等于:nextscreen.display = mainmidlet.display;
这样,nextscreen就得到了当前的屏幕,它就随意的在上面放东东了。
parent = p;
这一句其实等于:nextscreen.parent = mainmidlet.form;
从字面意思不难理解,原来是把主程序的form当成parent(父母),这样就得到当前屏幕的前一个屏幕中所显示的内容!!
然后在commandaction中,如果backcommand按下了,则执行display.sercurrent(parent);这样,又把原来的屏幕给show出来了:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐