【发布时间】:2012-03-13 06:56:47
【问题描述】:
我想验证在文本框中输入的数字。
我希望用户在最大值和最小值之间的框中只输入整数、小数。
我怎样才能确定这一点?
谢谢。
【问题讨论】:
标签: text swt eclipse-rcp
我想验证在文本框中输入的数字。
我希望用户在最大值和最小值之间的框中只输入整数、小数。
我怎样才能确定这一点?
谢谢。
【问题讨论】:
标签: text swt eclipse-rcp
使用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;
}
}
});
【讨论】:
您可以向文本控件注册一个 ModifyListener 并使用它来验证数字。
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;
}
}
}
});
【讨论】:
试试下面的代码:
/**
* Verify listener for Text
*/
private VerifyListener verfyTextListener = new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
String string = e.text;
Matcher matcher = Pattern.compile("[0-9]*+$").matcher(string);
if (!matcher.matches()) {
e.doit = false;
return;
}
}
};
【讨论】:
如果您只想允许整数值,您可以使用 Spinner 而不是文本字段。
对于 Double 值的验证,您必须考虑像 E 这样的字符可能包含在 Doubles 中,并且像“4e-”这样的部分输入在键入时应该是有效的。这样的部分表达式将为 Double.parseDouble(partialExpression) 提供 NumberFormatException
另请参阅以下相关问题的回答: How to set a Mask to a SWT Text to only allow Decimals
【讨论】:
1.创建一个utill类实现验证监听
2.覆盖验证文本方法
3.实现你的逻辑
4.创建一个 utill 类的对象,您要在其中使用验证侦听器(在文本上)
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);
} }
【讨论】:
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;
}
});
【讨论】: