Codename One - addActionListener of a Picker

折月煮酒 提交于 2019-12-08 06:19:25

问题


The following code, in a lot of version of Android and iOS, shows the options "Cancel" and "OK". I would like that the addActionListener code is executed only when the user taps on the "OK" button. At the moment, that code is executed even if the user taps the "Cancel" button:

    Form hi = new Form("Hi World", BoxLayout.y());
    Picker languagePicker = new Picker();
    languagePicker.setType(Display.PICKER_TYPE_STRINGS);
    languagePicker.setStrings("Italian", "English", "German");
    languagePicker.setSelectedString("English");
    languagePicker.addActionListener((ev) -> {
        String newLanguage = languagePicker.getSelectedString();
        if (newLanguage != null && newLanguage.length() > 0) {
            Log.p("Language selected: " + newLanguage);
        }
    });
    hi.add(languagePicker);
    hi.show();

回答1:


The Picker is a native component. As a result we just don't have that level of access or consistency when working with it. Keep in mind that in some forms it won't even have a list of elements so our control is very limited.




回答2:


I use the picker on several Android devices and they work as expected, meaning the action is performed only when the OK-Button is pressed.

Pressing the Cancel-Button does not incur in any action.

I could reproduce the error in the simulator, though.

Have you tried it on a device?



来源:https://stackoverflow.com/questions/48758348/codename-one-addactionlistener-of-a-picker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!