简体   繁体   English

getTxt()和SWT Spinner的getSelection()有什么区别?

[英]What is the difference between getText() and getSelection() of SWT Spinner?

For a Spinner control in SWT, what is the difference between getText() and getSelection() ? 对于SWT中的Spinner控件, getText()getSelection()之间有什么区别? The only thing I can make out is the return type. 我唯一能说的就是返回类型。 However, I cannot enter any text, only numbers are allowed. 但是,我无法输入任何文字,只允许输入数字。

getText(): Returns a string containing a copy of the contents of the receiver's text field, or an empty string if there are no contents. getText():返回包含接收者文本字段内容副本的字符串,如果没有内容则返回空字符串。

getSelection(): Returns the selection, which is the receiver's position. getSelection():返回选择,即接收者的位置。

So why do we have two different getters and which one should I use? 那么为什么我们有两个不同的吸气剂,我应该使用哪一个?

Since the Spinner represents a numeric selection, you should go with getSelection() as it returns exactly what has been selected. 由于Spinner表示数字选择,因此您应该使用getSelection()因为它会准确返回已选择的内容。

I don't know why getText() exists, but it'll return the String representation of the selected value, eg if getSelection() returns the int 7 , getText() will return the String "7" . 我不知道为什么getText()存在,但它将返回所选值的String表示,例如,如果getSelection()返回int 7 ,则getText()将返回String "7"

As the documentation states, getText() returns the string as if is currently displayed, including possible non-numeric values or values outside the min/max range. 正如文档所述, getText()返回当前显示的字符串,包括可能的非数字值或最小/最大范围之外的值。 getSelection() returns the current or last valid integer value. getSelection()返回当前或最后一个有效的整数值。

Usually, you would want to use getSelection() . 通常,您可能希望使用getSelection() Does that answer your question? 这是否回答你的问题?

  1. getText specifically says it can be empty (I'd guess it depends on platform whether this ever happens). getText具体说它可以是空的(我猜它依赖于平台是否会发生这种情况)。 It then allows to distinguish this case from value 0. 然后它允许将此情况与值0区分开。

  2. You could use getText when using Spinner with decimal digits (see http://help.eclipse.org/luna/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/Spinner.html#setDigits(int) ), since getSelection in this case is farther from the meaning of the value. 使用带有十进制数字的Spinner时可以使用getText (请参阅http://help.eclipse.org/luna/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/Spinner。 html#setDigits(int) ),因为在这种情况下getSelection远离值的含义。 But note that the decimal separator depends on the current locale. 但请注意,小数分隔符取决于当前的区域设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM