【问题标题】:Codename One set action event from different class for button in dialog代号 来自不同类的一组动作事件,用于对话框中的按钮
【发布时间】: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


    【解决方案1】:

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

    两种解决方案:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多