我希望验证文本框中的数字输入。
我希望用户只能在最大值和最小值之间的框中输入整数或小数。
我该如何确保这一点?
谢谢。
使用 VerifyListener
,它可以处理粘贴、退格、替换等操作......
例如:
text.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
final String oldS = text.getText();
final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
try {
BigDecimal bd = new BigDecimal(newS);
// value is decimal
// Test value range
} catch (final NumberFormatException numberFormatException) {
// value is not decimal
e.doit = false;
}
}
});
txt.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent event) {
String txt = ((Text) event.getSource()).getText();
try {
int num = Integer.parseInt(txt);
// Checks on num
} catch (NumberFormatException e) {
// Show error
}
}
});
您还可以使用addVerifyListener来防止输入某些字符。在传递到该方法的事件中,有一个“doit”字段。如果将其设置为false,则可以阻止当前编辑。
SWT中的整数验证
text = new Text(this, SWT.BORDER);
text.setLayoutData(gridData);
s=text.getText();
this.setLayout(new GridLayout());
text.addListener(SWT.Verify, new Listener() {
public void handleEvent(Event e) {
String string = e.text;
char[] chars = new char[string.length()];
string.getChars(0, chars.length, chars, 0);
for (int i = 0; i < chars.length; i++) {
if (!('0' <= chars[i] && chars[i] <= '9')) {
e.doit = false;
return;
}
}
}
});
text.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
Text text = (Text) e.getSource();
final String oldS = text.getText();
String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
boolean isValid = true;
try {
if(! "".equals(newS)){
Float.parseFloat(newS);
}
} catch (NumberFormatException ex) {
isValid = false;
}
e.doit = isValid;
}
});
如果您只想允许整数值,可以使用Spinner而不是文本字段。
对于Double值的验证,您必须考虑到像E这样的字符可能包含在Doubles中,并且像“4e-”这样的部分输入应该在键入时有效。这些部分表达式将为Double.parseDouble(partialExpression)提供NumberFormatException。
还请参阅我在以下相关问题的答案: 如何将掩码设置为SWT文本以仅允许小数
1. 创建一个实用程序类实现验证监听器
2. 覆盖verifytext方法
3. 实现您的逻辑
4. 在需要使用验证监听器(对于文本)的地方创建一个实用程序类的对象
5.text.addverifylistener(utillclassobject)
例子: 1. 工具类:
public class UtillVerifyListener implements VerifyListener {
@Override
public void verifyText(VerifyEvent e) {
// Get the source widget
Text source = (Text) e.getSource();
// Get the text
final String oldS = source.getText();
final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
try {
BigDecimal bd = new BigDecimal(newS);
// value is decimal
// Test value range
} catch (final NumberFormatException numberFormatException) {
// value is not decimal
e.doit = false;
}
}
2.在其他类中使用verifylistener
public class TestVerifyListenerOne {
public void createContents(Composite parent){
UtillVerifyListener listener=new UtillVerifyListener();
textOne = new Text(composite, SWT.BORDER);
textOne .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
textOne .addVerifyListener(listener)
textTwo = new Text(composite, SWT.BORDER);
textTwo .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
textTwo .addVerifyListener(listener);
} }