如何在CN1中编写iPhone风格的弹出菜单?

时间:2016-10-04 17:38:34

标签: codenameone

之前可能已被覆盖,但我无法谷歌任何东西。制作iPhone风格的弹出式选择菜单(如附图)的最佳方法是什么?我已尝试使用Dialog,但我还没有找到一种优雅的方法来添加Commands,因此它们看起来很漂亮,同时触发操作并同时关闭对话框。 ComponentGroup不支持单独显示“取消”条目。

enter image description here

3 个答案:

答案 0 :(得分:1)

请参阅此示例:

Form hi = new Form("Pop");
Button pop = new Button("Pop");
pop.addActionListener(e -> {
    Dialog dlg = new Dialog();

    // makes the dialog transparent
    dlg.setDialogUIID("Container");
    dlg.setLayout(BoxLayout.y());

    Command optionACmd = new Command("Option A");
    Command optionBCmd = new Command("Option B");
    Command optionCCmd = new Command("Option C");
    Command cancelCmd = new Command("Cancel");

    dlg.add(
            ComponentGroup.enclose(
                    new Button(optionACmd), 
                    new Button(optionBCmd), 
                    new Button(optionCCmd)
                    )).
            add(ComponentGroup.enclose(new Button(cancelCmd)));

    Command result = dlg.showStretched(BorderLayout.SOUTH, true);
    ToastBar.showMessage("Command " + result.getCommandName(), FontImage.MATERIAL_INFO);
});
hi.add(pop);
hi.show();

结果如下:

iOS style Popup

答案 1 :(得分:1)

谢谢Shai!

我把它变成了一个组件,以防任何人有类似的需求:

class MyPopupMenu extends Dialog {

    private Command cancelCmd = null;

    MyPopupMenu(boolean includeCancel, Command... commands) {
        this(includeCancel?new Command("Cancel"):null, commands);
    }

    MyPopupMenu(Command cancelOptional, Command... commands) {
        super();
        setDialogUIID("Container");
        setLayout(BoxLayout.y());
        setDisposeWhenPointerOutOfBounds(true); //close if clicking outside menu
        ComponentGroup group = new ComponentGroup();
        for (Command cmd : commands) {
            group.add(new Button(cmd));
        }
        add(group);

        this.cancelCmd = cancelOptional;
        if (cancelCmd != null) {
            add(ComponentGroup.enclose(new Button(cancelCmd)));
        }


    /**
     * show the menu and execute the selected Command, 
     * or do nothing if Cancel is selected
     */
    public void popup() {
        Command choice = showStretched(BorderLayout.SOUTH, true);
        if (choice != null && choice != cancelCmd) {
            choice.actionPerformed(null);
        }

    }
}

答案 2 :(得分:0)

这太棒了,谢谢你们。我的按钮看起来如此之小的任何原因?试图找出需要改变的样式以增加高度。更改按钮填充似乎没有任何改变。我以商业主题为出发点。 enter image description here