【发布时间】:2020-10-04 02:57:03
【问题描述】:
我有一个 Android 应用程序,里面有一个对话框和几个按钮。
我想将对话框重用于不同的目的,并寻找一种从单独的类中调用按钮并为其定义动作事件的方法。
创建了一个测试类,我设法为表单内的按钮定义了一个动作事件,但相同的代码不适用于对话框内的按钮,我无法理解为什么它不工作对于对话框。
以下是我已经拥有的。任何建议,将不胜感激。
public Class One {
Test test = new Test();
test.testOne(); // this is working : button prints
test.testTwo(); // this is not working : button does not print
buttonTest = test.getTestButton();
buttonTest.setText("Hello World"); // not working for a button in a dialog
buttonTest.addActionListener(l-> { // prints when the button in a Form
System.out.println("try"); // does not print when the button is in a dialog
});
}
public class Test {
Dialog dialog = new Dialog();
Form form = new Form();
Button button;
public void testOne() {
button = new Button("Test");
form.add(button);
form.show();
}
public void testTwo() {
button = new Button("Testing");
dialog.add(button);
dialog.show();
}
public Button getTestButton () {
return button;
}
}
【问题讨论】:
标签: dialog codenameone actionevent