【问题标题】:Codename One - event listener for Dialog dismiss/cancel/tap outside代号一 - 对话框关闭/取消/点击外部的事件侦听器
【发布时间】:2020-06-10 01:45:07
【问题描述】:

我正在通过 Codename One 移植我的 Android 应用程序。

现在我正在将对话框移植到我的应用程序中。

我能够在 Codename 项目中创建类似的对话框,将 ActionListeners 添加到按钮等,但我无法找到用于取消/关闭/点击外部事件的事件侦听器。

dispose() 方法没有相应的侦听器,这会很有用。

这是最简单的对话框,但我也有更复杂的:

public static void openAlertDialog( String s1, String s2)
{
   Dialog alertDialog=new Dialog(s1);
    Button okButton=new Button("ok");
    alertDialog.setLayout(BoxLayout.y());
    Container c1=new Container(); //not so useful here but when there are more buttons
    c1.setLayout(BoxLayout.x());
    alertDialog.add(new SpanLabel(s2, "DialogBody"));
    c1.add(okButton);
    alertDialog.add(c1);
    alertDialog.show();
}

当对话框关闭但没有按下按钮时,如何有机会执行一些代码?

【问题讨论】:

    标签: java android codenameone actionlistener


    【解决方案1】:

    Codename One 对话框甚至不需要事件侦听器。例如。这段代码可以这样写:

    Dialog alertDialog=new Dialog(s1);
    Command ok = new Command("ok");
    Button okButton=new Button(ok);
    alertDialog.setLayout(BoxLayout.y());
    Container c1=new Container(); //not so useful here but when there are more buttons
    c1.setLayout(BoxLayout.x());
    alertDialog.add(new SpanLabel(s2, "DialogBody"));
    c1.add(okButton);
    alertDialog.add(c1);
    alertDialog.setDisposeWhenPointerOutOfBounds(true);
    if(alertDialog.showDialog() == ok) {
        // user pressed OK. You can test against other commands than ok as well
    } else {
        // canceled or clicked outside
    }
    

    【讨论】:

    • 这个很简单,但是编译器说show()返回void。
    • 对不起,我的错误。忘记修复调用使用showDialog() 纠正了示例中的错误
    猜你喜欢
    • 1970-01-01
    • 2011-11-30
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多