天天看點

J2ME手機螢幕的切換

在編寫手機程式時,經常要進行各個螢幕間的切換

功能:在主程式中有多個按鈕,每一個按鈕對應一個功能,每一個功能要不同的螢幕(元素)表現出來。

實作:

一、主程式中必然定義了一個display對像,如private Display display,它表示目前的螢幕。還有一些Displayable對像。如form,textfield等都是displayable的子類。在主程式中通過dipslay.sercurrent(displayable執行個體名); 即可将當displayable執行個體加入目前的螢幕。以下程式:

  1. private Display display=Display.getDisplay(this); 
  2. private Form form = new Form(“New Form“); 
  3. public void startapp() 
  4. display.setcurrent(form); 
  5. }

作用是将form添加到目前的螢幕當中。

二、要想進行螢幕間的切換,隻要将你想顯示的東東放到到主程式的display對象中即可。主程式中定義了一個display,則要在另一個螢幕(我姑且把它稱之為目标螢幕)中引用到主程式的display。

用以下代碼說明:

mainmidlet.java:主程式,一個标準的midlet。

  1. import javax.microedition.midlet.midlet; 
  2. import javax.microedition.lcdui.*;
  3. public class mainmidlet extends midlet implements commandlistener {
  4. private display display; 
  5. private form form = new form("wellcome!!"); 
  6. private command okcommand = new command("ok",command.ok,1);
  7. //選擇ok,換到下一個螢幕 
  8. private form ns ; 
  9. private stringitem si = new stringitem("first screen","~_~"); 
  10. public mainmidlet() 
  11.     form.addcommand(okcommand); 
  12.     form.append(si); 
  13. public void startapp() { 
  14.     display = display.getdisplay(this);
  15.     display.setcurrent(form); 
  16.     form.setcommandlistener(this);//對form加入commandlistener
  17. public void pauseapp() {
  18. public void destroyapp(boolean b){
  19. public void commandaction(command c,displayable s) 
  20.     if(c==okcommand) 
  21.     { 
  22.         ns = new nextscreen(display,form);//最關鍵的地方在這裡:form:将父螢幕對象傳下去,友善後退時傳回父螢幕) 
  23.         display.setcurrent(ns); 
  24.     }
  25. }

在這個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 第二個螢幕的代碼

  1. import javax.microedition.lcdui.*; 
  2. public class nextscreen extends form implements commandlistener {
  3. private display display; 
  4. private displayable parent; 
  5. private command backcommand = new command("back",command.back,1); 
  6. private stringitem si = new stringitem("secondscrean","~_~"); 
  7. public nextscreen(display d,displayable p) 
  8.     super("nextscreen"); 
  9.     display = d; 
  10.     parent = p; 
  11.     append(si); 
  12.     addcommand(backcommand); 
  13.     setcommandlistener(this);
  14. public void commandaction(command c,displayable s) 
  15. //傳回上一個螢幕 
  16.     if(c==backcommand) 
  17.     { 
  18.     display.setcurrent(parent); 
  19.     } 
  20. }

它繼承自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出來了:)