为什么我们需要TextFormatters的转换器 [英] Why do we need converters for TextFormatters

查看:267
本文介绍了为什么我们需要TextFormatters的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授说,为TextFormatter使用过滤器和转换器是理想的。我查看了他的例子并尝试了它们,但无法理解为什么我们需要转换器。

My professor said that it is 'ideal' to use a filter and a converter for a TextFormatter. I looked at his examples and tried them, but couldn't understand why we need a converter at all.

来自 docs


Formatter通过使用两种不同的机制描述TextInputControl文本的格式:

A Formatter describes a format of a TextInputControl text by using two distinct mechanisms:

可以拦截和修改用户输入的过滤器(getFilter()) 。这有助于使文本保持所需的格式。默认文本
供应商可用于提供初始文本。

A filter (getFilter()) that can intercept and modify user input. This helps to keep the text in the desired format. A default text supplier can be used to provide the intial text.

值转换器(getValueConverter())和值( valueProperty())可用于提供特殊格式,
表示类型为V的值。如果控件是可编辑的,并且用户更改了文本
,则更新该值以对应于
text。

A value converter (getValueConverter()) and value (valueProperty()) can be used to provide special format that represents a value of type V. If the control is editable and the text is changed by the user, the value is then updated to correspond to the text.

我很清楚这里缺少的东西。我明白为什么要将字符串转换为整数(用于计算等)。但为什么你必须把它作为TextFormatter的一部分?我们不能只使用getText()然后只是转换文本,因为我们想要有值?

I am cleary something missing here. I get why you want to convert a string to an integer (for calculations etc.). But why do you have to have it as a part of TextFormatter? Can't we just use getText() and then just cast the text as we want to have the value?

还有一件事:如果我们有一个不允许非数字字符的过滤器,那么为什么我们需要处理文本到整数的转换/ double等转换器?

One more thing: If we have a filter that doesn't allow non-numeric characters, then why do we need to take care of the conversion of the text to integer/double etc. with a converter?

也许我只是遗漏了一些非常明显的东西。

Maybe I am just missing something very obvious.

推荐答案

您不能将字符串转换为 Integer (或任何其他类型, Object 除外):您必须转换它。即使文本格式化程序有一个只允许数字输入的过滤器,文本字段的 getText()方法仍然会返回一个字符串,这通常不是很方便(作为条目)文本字段可能表示某个对象中的数值。)

You can't cast a String to an Integer (or any other type, except an Object): you have to convert it. Even if the text formatter has a filter that only allows numeric entry, the text field's getText() method still returns a string, which is usually not very convenient (as the entry in the text field likely represents a numeric value in some object).

您可能需要获取许多不同位置的文本字段所表示的整数(例如)值,因此,通过将转换器作为格式化程序的一部分包含在内,可以将转换代码集中在一个位置。

You might need to get the integer (for example) value represented by the text field in many different places, so you centralize the conversion code in one place by including the converter as part of the formatter.

此外,格式化程序的值是一个可观察的属性,因此您可以轻松地绑定其他如果你需要在文本字段的文本属性的绑定中执行转换,这将是棘手的。

Additionally, the formatter's value is an observable property, so you can easily bind other properties to it, etc. This would be tricky if you needed to perform the conversion in a binding on the text field's text property.

这篇关于为什么我们需要TextFormatters的转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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