简体   繁体   English

在SWT中验证文本并尝试清除它,但不起作用

[英]Verifying a Text in SWT and trying to clear it but doesn't work

I am trying to verify a text in SWT to allow only binary numbers and it works, but my problem is that I have a button that I need to reset the field but it doesn't work. 我正在尝试验证SWT中的文本以仅允许二进制数字,并且它可以工作,但是我的问题是我有一个按钮需要重置该字段,但它不起作用。

Here is the code 这是代码

DACMUX.addVerifyListener(new VerifyListener() {

            public void verifyText(VerifyEvent e) {
                e.doit = false;

                char myChar = e.character;
                if (myChar == '1' || myChar == '0' || myChar == '\b'){
                    e.doit = true;
                }
            }
        });

reset.addSelectionListener(new SelectionListener() {

            @Override
            public void widgetSelected(SelectionEvent e) {
            DACMUX.setText(""); 

            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                // TODO Auto-generated method stub

            }
        });

Interesting case you got there. 有趣的情况,你到了那里。 Took me a while to figure it out. 花了我一段时间才能弄清楚。

Right, so here goes: The VerifyListener is fired after you call setText("") . 没错,这里是这样:在调用setText("")之后,将触发VerifyListener It checks your condition and it validates to false , so nothing changes. 它检查您的状况,并验证为false ,因此什么都不会改变。

To make it work, you need to add one additional OR case to your if , which checks if Event#text equals "" . 为了使其工作,您需要在if添加一个额外的OR大小写,以检查Event#text等于"" This is the case when you call setText("") . 调用setText("")时就是这种情况。 It won't mess up you other logic. 它不会弄乱您其他逻辑。

So here is the working if : 所以这是工作的情况, if

if (myChar == '1' || myChar == '0' || myChar == '\b' || e.text.equals(""))
{
    e.doit = true;
}

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

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