motorola手機中在主類中響應其它類中的按鈕訊息 (轉)

worldblog發表於2007-12-13
motorola手機中在主類中響應其它類中的按鈕訊息 (轉)[@more@]

 

見 :

//名 LWTDemoMIDlet.
import com.motorola.lwt.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class LWTDemoMIDlet extends MIDlet implements CommandListener
{
class ButtonScreen extends ComponentScreen {

public ButtonScreen() {

// Add a button to the screen
Button b1 = new Button("b");
///////////下面的程式碼是設定按鈕高度
b1.setBottomEdge(Component.HEIGHT, 30);

///////////下面的程式碼是設定按鈕寬度
b1.setRightEdge(Component.WIDTH,80);

///////////下面的程式碼是設定按鈕y座標,a相對螢幕最上

b1.setTopEdge(Component.SCREEN_TOP, 10);

///////////下面的程式碼是設定按鈕x座標相對前一個
b1.setLeftEdge(Component.PREVIOUS_COMPONENT_RIGHT, 30);


add(b1);


// Add another button to the screen
Button b2 = new Button("點我就行了"){
public void componentActuated() {
Show();
}
};
// Extend the right edge to the right edge of the screen
b2.setRightEdge(Component.WIDTH, 220);
b2.setBottomEdge(Component.HEIGHT, b2.getPreferredHeight() * 2);
add(b2);
Command next = new Command("Next", Command.OK, 1);
Command prev = new Command("Previous", Command.BACK, 1);

addCommand(next);
addCommand(prev);
}
};

ButtonScreen screens = new ButtonScreen();
public LWTDemoMIDlet() {
screens.setCommandListener(this);
}
protected void startApp()
throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(screens);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException
{
}
public void Show()
{
System.out.println("Get it on main Class");


}
public void commandAction (Command c, Displayable d)
{

if (screens == d) {
// Found it, check which command was triggered
if (c.getCommandType() == Command.BACK) {
System.out.println("back");
}
else if (c.getCommandType() == Command.OK) {
System.out.println("ok");
}
return;
}

}

}

以上程式用jcreater pro

motoSDK 編譯


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992715/,如需轉載,請註明出處,否則將追究法律責任。

相關文章