【发布时间】:2020-05-09 16:41:25
【问题描述】:
所以,我有一个带有 A 和 B 两个类的 Android 应用程序。返回命令的逻辑在 A 类中设置并按预期工作:按钮操作显示 Form B,而 Escape 按钮带来 Form A返回。
现在,在 B 类中加载表单后,我需要“重新加载”它。我通过创建一个新表单,通过一个按钮再次调用整个类来做到这一点。
在此过程中,后退命令停止工作(命令按钮出现在工具栏中),因为所有组件(工具栏、表单等)都是新创建的,并且缺少 A 类的定义。
我的问题:是否可以在 B 类而不是 A 类中定义后退命令逻辑,以便在重新加载 B 类后后退命令仍然有效,或者以某种方式定义对 A 类的引用以使其工作?
下面是我已经拥有的代码。非常感谢您的任何建议。
public class ClassA {
private Command back;
private ClassB classB;
private Form current;
private Resources theme;
private Form formA;
private Button button = new Button("Go forward");
public void loadA() {
showBack();
formA = new Form("Old Form", BoxLayout.y());
classB = new ClassB();
button.addActionListener(l -> {
classB = new ClassB();
classB.formB.getToolbar().setBackCommand(back);
classB.formB.setBackCommand(back);
classB.goBack();
Button buttonToolbar = classB.formB.getToolbar().findCommandComponent(back);
FontImage image = FontImage.createMaterial(FontImage.MATERIAL_10K, "test", 4);
buttonToolbar.setIcon(image);
});
formA.add(button);
formA.show();
}
public void showBack() {
back = new Command("Back") {
@Override
public void actionPerformed(ActionEvent evt) {
formA.showBack();
}
};
}
}
public class ClassB {
public ClassA classA = new ClassA();
public Form formB = new Form("New Form", BoxLayout.y());
private Button buttonReload = new Button("Reload");
public void reload () {
buttonReload.addActionListener(l -> {
ClassB classB = new ClassB();
classB.reload();
// I want to recreate the back-command logic here in Class B.
// The below doesn't work.
formB.getToolbar().setBackCommand(new Command("back"));
});
formB.add(buttonReload);
formB.show();
}
}
【问题讨论】:
标签: codenameone back