是否有任何理由为什么在LWUIT中Button可以拥有自己的ActionListener(通过button.addActionListener)而Command不具备?
获取特定命令的唯一方法是将ActionListener添加到表单并检查侦听器,该事件来自哪个Command,如下所示?
public void startApp() {
Display.init(this);
f = new Form("Mixed Record");
exit = new Command("Exit");
start = new Command("Start");
Button button = new Button("Button");
f.addCommand(exit);
f.addCommand(start);
f.addCommand(delete);
f.addComponent(button);
f.addCommandListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getCommand().equals(exit)) {
//Do Exit command code
} else if (ae.getCommand().equals(start)) {
//Do Start command code
}
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Do button code
}
});
f.show();
}
Run Code Online (Sandbox Code Playgroud)