Java SWT:输入验证:防止输入子和上标数字
我正在验证来自swt文本小部件的输入,但我无法阻止输入sub和superscript数字。我的目标是只允许数字输入。我使用的是SWT验证侦听器,我的代码如下:Java SWT:输入验证:防止输入子和上标数字,java,validation,input,swt,Java,Validation,Input,Swt,我正在验证来自swt文本小部件的输入,但我无法阻止输入sub和superscript数字。我的目标是只允许数字输入。我使用的是SWT验证侦听器,我的代码如下: [...] Text input = new Text (parent, SWT.CENTER); input.addListener(SWT.VERIFY, new Listener(){ public void handleEvent(Event e){ if ( (e.character >= '0' &&am
[...]
Text input = new Text (parent, SWT.CENTER);
input.addListener(SWT.VERIFY, new Listener(){
public void handleEvent(Event e){
if ( (e.character >= '0' && e.character <= '9') || e.character == SWT.BS || e.character == SWT.DEL){
e.doit = true;
} else {
e.doit = false;
}
}
});
[...]
也许有一种简单直接的方法可以做到这一点,但我没有看到,谷歌也没有提供答案
有人知道如何做到这一点吗?与此同时,我找到了一个适合我的解决方案;我把它贴在这里,希望它能帮助别人
input = new Text(parent, style);
input.addVerifyListener(new VerifyListener(){
e.doit = false;
String DIGIT = "[0-9]*";
if (e.text.matches(DIGIT)){
e.doit = true;
}
});
这个解决方案确实允许前导零,但就我而言,这不是一个问题。我不知道,为什么在算术上比较字符不起作用,但e.text及其matches方法工作得很好。这是您要问的Unicode超级和下标数字(例如下标0的U+2080)?
input = new Text(parent, style);
input.addVerifyListener(new VerifyListener(){
e.doit = false;
String DIGIT = "[0-9]*";
if (e.text.matches(DIGIT)){
e.doit = true;
}
});