如何使文本阻止输入超过“x”位数?
我的问题是:
我希望 Eclipse RCP 中的文本字段只允许输入数字作为输入,并且要求它最多应该允许“x”(假设 x=5)个字符。
我怎样才能在 RCP 中完成它?
我尝试过如下代码:
txtInput.addListener(SWT.Verify, new DecimalText(
txtInsuranceValue, 8, 1000.00));
其中 txtInput 是一个文本字段。但是当我在运行时重复输入该字段时,该监听器失败了。
请问还有什么替代方案吗?
My question is:
I want the Text field in Eclipse RCP to allow only the digits to be keyed in as inputs and the requirement is that it should allow only up to 'x' (let's say x=5) characters.
How can I accomplish it in RCP ?
I've tried the code like:
txtInput.addListener(SWT.Verify, new DecimalText(
txtInsuranceValue, 8, 1000.00));
where txtInputis a Text field. But this listener failed when I made repeated input into this field at run time.
Any alternatives please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会将文本控件中的最大字符数设置为五个,并且只允许输入数字:
编辑:我已经修改了我的答案,以允许用户也使用“设置文本” setText(String string)”,同时仍然不允许非数字字符。
This will set the maximum number of characters in your Text control to five and allow only digits to be entered:
EDIT: I've modified my answer to allow a user to also set text with "setText(String string)" while still disallowing non-digit characters.
continue
In general, solutions like adding verifyListeners/focusListeners are not complete and won't work with all situations. For example, what happens when the user copy-pastes some text into this text box?
If all you want is to get numerical inputs from the user, you should be using Spinner widget instead of Text widget. You could easily set the minimum and maximum limits on the Spinner.
See the snippets for more usage.