如何使文本阻止输入超过“x”位数?

发布于 2024-11-07 16:56:41 字数 321 浏览 3 评论 0原文

我的问题是:

我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-11-14 16:56:41

这会将文本控件中的最大字符数设置为五个,并且只允许输入数字:

    txtInput.setTextLimit(5);

    txtInput.addVerifyListener(new VerifyListener() {
        public void verifyText(VerifyEvent event) {
            // Assume we allow it
            event.doit = true;

            String text = event.text;
            char[] chars = text.toCharArray();

            //Don't allow if text contains non-digit characters
            for (int i = 0; i < chars.length; i++) {
                if (!Character.isDigit(chars[i])) {
                    event.doit = false;
                    break;
                }
            }
        }
    });

编辑:我已经修改了我的答案,以允许用户也使用“设置文本” setText(String string)”,同时仍然不允许非数字字符。

This will set the maximum number of characters in your Text control to five and allow only digits to be entered:

    txtInput.setTextLimit(5);

    txtInput.addVerifyListener(new VerifyListener() {
        public void verifyText(VerifyEvent event) {
            // Assume we allow it
            event.doit = true;

            String text = event.text;
            char[] chars = text.toCharArray();

            //Don't allow if text contains non-digit characters
            for (int i = 0; i < chars.length; i++) {
                if (!Character.isDigit(chars[i])) {
                    event.doit = false;
                    break;
                }
            }
        }
    });

EDIT: I've modified my answer to allow a user to also set text with "setText(String string)" while still disallowing non-digit characters.

七色彩虹 2024-11-14 16:56:41

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文