代号一-正确使用选择器 [英] Codename One - Correct use of the Picker

查看:61
本文介绍了代号一-正确使用选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对使用Picker提出一个明确的问题,因为我的上一个问题不够清楚:代号1-选取器的addActionListener

I'm trying to do a clear question about the use of the Picker, because my previous one in not enough clear: Codename One - addActionListener of a Picker

选取器的目的是从集合中选择一个元素,例如一组字符串中的一个字符串,对吗?因此,我想让用户有机会从一组字符串中选择一个字符串,每个字符串对应一种语言。

The purpose of the Picker is to select one element from a set, such as a string of a set of strings, is it right? So, I want to give the user the opportunity to select a string from a set of strings, each of one corresponds to a language.

问题是使用添加到选择器中的ActionListener不适用于此目的,因为它是由确定和取消按钮触发的。

The problem is that the use of an ActionListener added to the Picker is not correct for this purpose, because it's triggered by both "Ok" and "Cancel" buttons.

我需要执行一些代码( (并且仅在用户按下确定时才更改语言),因为如果在用户按下取消时执行该代码应被视为意外行为和错误(从用户的角度来看)。

I need to execute some code (to change the language) only and only if the user press "Ok", because executing that code if the user press "Cancel" should be considered an unexpected behavior and a bug (from the point of view of the user).

所以我的问题是在此用例中如何正确使用选择器。

So my question is how to use correctly the Picker in this use case.

推荐答案

我必须这样做,以避免在按下取消的情况下触发字符串上的另一个监听器(我正在使用带有更改监听器的属性),我通过对照先前选择的字符串检查当前选择器字符串以查看其是否改变来做到这一点。如果值更改,则仅更新字段或局部变量(在我的情况下为属性)。修改上一个答案的代码:

I had to do this to avoid triggering another listener on the string (I was using a Property with a change listener) if Cancel was pressed, and I did this by checking the current picker string against the previous selected string to see if it changed. I only updated the field or local variable (Property in my case) if the value changed. Modifying the previous answer's code:

...
private String currentSelection;
...

String [] list = {"one" , "two" , "three"};
Picker picker = new Picker ();
picker.setType(Display.PICKER_TYPE_STRINGS);
picker.setStrings(list);
picker.setSelectedString("test");

picker.addActionListener(l -> {
   if (!picker.getSelectedString.equals(currentSelection) {
      currentSelection = picker.getSelectedString;
      System.out.println(currentSelection);
   }
});

这篇关于代号一-正确使用选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆