GWT 中数字文本字段的解决方案
我需要一个行为与 Gxt 的 NumberField 非常相似的文本字段。不幸的是,我没有在我的应用程序中使用 Gxt,并且 GWT 2.0 还没有数字文本字段实现。
因此,目前我可以选择通过使用键盘处理程序过滤掉非数字击键来模拟 NumberField。
这是解决问题的最佳方法吗?这里有人有更好的解决方案/方法吗?
提前致谢 :)
I need a text field very similar in behavior to Gxt's NumberField. Unfortunately I am not using Gxt in my application and GWT 2.0 does not have a Numeric text field implementation as yet.
So that currently leaves me with an option to simulate a NumberField by filtering out non-numeric keystrokes using a keyboardHandler.
Is this the the best way to approach the problem? Does anyone here have a better solution/approach in mind?
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这里您可以找到我在我的一门课程中使用的代码。其功能比 GXT 的功能要有限得多,但应该能让您走上正确的轨道。
这是一个非常基本的小部件,但可以满足我的需要。
更新:代码位于 https:// github.com/ctasada/GWT-Eureka
Here you can find the code that I use in one of my classes. The features are much more limited that those of GXT, but should put you in the proper track.
It's a really basic widget, but does what I need to.
Update: The code is available in https://github.com/ctasada/GWT-Eureka
这是一个简单的 KeyPressHandler,允许用户输入十进制数字;
Here is a simple KeyPressHandler to allow the user to input decimal numbers;
不知道这些类何时添加到 GWT,但它们对我来说工作得很好,不需要任何额外的代码:
对于更高级的验证,您可能需要使用一些自定义解析器覆盖它们的基类 ValueBox...
Don't know when these classes were added to GWT, but they work fine for me without any extra code:
For more advanced validation you may want to overwrite their base class ValueBox with some custom Parser...
Carlos Tasada 答案有效,但包含一个错误:您应该在 switch/case 块之前在 onKeyPress 处理程序中添加 event.isShiftKeyDown() 检查。否则它会传递一些像“(”这样的符号。
Carlos Tasada answer works, but contains a bug: you should add event.isShiftKeyDown() check in onKeyPress handler before switch/case block. It will pass some symbols like '(' otherwise.
根据朱利安唐斯的回答,你可以这样做:
Based on Julian Downes answer you can do this:
这是我的 NumberField 实现。功能与卡洛斯的版本非常相似,但额外支持十进制输入和非数字键过滤。
PS
:超类 TextBox 是一个自定义类,它扩展了 GWT TextBox,并具有一些附加的应用程序特定功能。方法initialize() 基本上是在TextBox 构造函数内部调用的,而getSanitizedValue 通过修剪进行一些基本的健全性检查。
Here is my implementation of NumberField. Very similar in functionality to Carlos's version, but with additional support for decimal input and non-numeric key filtering.
}
PS: Superclass TextBox is a custom class extending GWT TextBox with some additional application specific features. The method initialize() is basically invoked inside the TextBox constructor, and getSanitizedValue does some basic sanity checks with trimming.