Codename对话框中按钮的不同类的一组动作事件

问题描述 投票:1回答:1

我有一个带对话框和几个按钮的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
1个回答
0
投票

您在显示表单和对话框后添加动作侦听器。这对于表单来说不是问题,因为表单show方法将继续。但是对话框show()方法将阻止。

两种解决方案:

  • 将侦听器绑定移到代码中较高的位置(在显示之前),这可能是个问题,因为该按钮尚不存在,因此您需要进行一些重构。

  • 将对话框上的show()调用更改为showModless()

© www.soinside.com 2019 - 2024. All rights reserved.