[常見問題] J2ME手機屏幕的切換
在編寫手機程序時,經(jīng)常要進行各個屏幕間的切換
功能:在主程序中有多個按鈕,每一個按鈕對應(yīng)一個功能,每一個功能要不同的屏幕(元素)表現(xiàn)出來。
實現(xiàn):
一、主程序中必然定義了一個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); //最關(guān)鍵的地方在這里:form:將父屏幕對象傳下去,方便后退時返回父屏幕)
display.setcurrent(ns);
}
}
}
在這個midlet中,定義了一個display對像display。
以及一個兩個displayable對象form form及stringitem si。運行后顯示在屏幕當中。
還有個一command okcommand,其作用是觸發(fā)下一個屏幕。
在public void commandlistener中可以看到,當當前按下的按鈕是okcommand時,初始化一個nextscreen對象
ns = new nextscreen(display,form);
將display和form傳入,作用就是進行屏幕的切換。
下面是nextscreen的實現(xiàn):
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。這是當前屏幕中要顯示的東東:)
構(gòu)造函數(shù)中的super("secondscreen"); 的作用是使得nextscreen可以直接調(diào)中其父類form中的函數(shù)。
backcommand的作用是返回上一個屏幕。
將form,si及backcommand加入nextscreen中,一個nextscreen的實例就完成了。在主程序(mainmidlet)中,就是ns。
接下來,最關(guān)鍵的地方,就是在mainmidlet中的這一句:display.setcurrent(ns);
就是把ns在當前的屏幕中顯示出來!這樣就可以看到nextscreen中定義的各個元素(form,si)了!
然后想返回原屏幕,怎么辦呢?這時nextscreen中的backcommand就起作用了。
仔細看這兩句:
在mainmidlet.java中:
ns = new nextscreen(display,form);
它將form也傳了進去。它有什么用呢?
在nextscreen的構(gòu)造函數(shù)中:
dispaly =d;
這一句其實等于:nextscreen.display = mainmidlet.display;
這樣,nextscreen就得到了當前的屏幕,它就隨意的在上面放東東了。
parent = p;
這一句其實等于:nextscreen.parent = mainmidlet.form;
從字面意思不難理解,原來是把主程序的form當成parent(父母),這樣就得到當前屏幕的前一個屏幕中所顯示的內(nèi)容!!
然后在commandaction中,如果backcommand按下了,則執(zhí)行display.sercurrent(parent); 這樣,又把原來的屏幕給show出來了。