Comdenameone Picker替代ComboBox

时间:2016-11-25 04:05:30

标签: combobox codenameone picker

我正在Codename One弄湿我的脚。我已经针对跨平台调查了更多其他选项,例如XamarinPhoneGapIonic,但我有点迷上Codename one,因为它真的编码一次并在任何地方运行。

我一直在浏览ui元素,我在填充combobox时遇到了阻碍(替代方案为Picker

假设我将商店作为价值对(storeIdstoreName)。我想在storeName中显示Picker,但保留storeId作为值参考。 选择商店后,我想将storeId传递给API调用 这可能吗。这可能是一个非常简单的问题,但似乎有点难以实现(我真的很擅长移动)。

谢谢。

1 个答案:

答案 0 :(得分:1)

我们的建议是avoid ComboBox。这是一种UI模式,在iOS上本身并不存在,并且在现代手机上会感到陌生。它存在于Codename One中。

在上面示例的代码中,您可以获得与复杂的多字段组合框类似的效果:

Form hi = new Form("Button", BoxLayout.y());

String[] characters = { "Tyrion Lannister", "Jaime Lannister", "Cersei Lannister"};
String[] actors = { "Peter Dinklage", "Nikolaj Coster-Waldau", "Lena Headey"};
int size = Display.getInstance().convertToPixels(7);
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(size, size, 0xffcccccc), true);
Image[] pictures = {
    URLImage.createToStorage(placeholder, "tyrion","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/tyrion-lannister-512x512.jpg"),
    URLImage.createToStorage(placeholder, "jaime","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/jamie-lannister-512x512.jpg"),
    URLImage.createToStorage(placeholder, "cersei","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/cersei-lannister-512x512.jpg")
};

MultiButton b = new MultiButton("Pick A Lanister...");
b.addActionListener(e -> {
    Dialog d = new Dialog();
    d.setLayout(BoxLayout.y());
    d.getContentPane().setScrollableY(true);
    for(int iter = 0 ; iter < characters.length ; iter++) {
        MultiButton mb = new MultiButton(characters[iter]);
        mb.setTextLine2(actors[iter]);
        mb.setIcon(pictures[iter]);
        d.add(mb);
        mb.addActionListener(ee -> {
            b.setTextLine1(mb.getTextLine1());
            b.setTextLine2(mb.getTextLine2());
            b.setIcon(mb.getIcon());
            d.dispose();
            b.revalidate();
        });
    }
    d.showPopupDialog(b);
});
hi.add(b);
hi.show();

enter image description here

如果您坚持使用ComboBox,则可以使用模型为其提供所需的任何对象数据。然后创建一个单元格渲染来显示数据。这些都在component section of Codname One's developer guide中进行了深入讨论。请注意,由于ComboBox派生自List,因此很多List提示和文档适用于ComboBox