嗨,我正在使用Eclipse Rcp我需要验证只接受整数值的文本框,因为我使用了代码
txtCapacity.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent EVT) {
if((EVT.character>='0' && EVT.character<='9')){
txtCapacity.setEditable(true);
txtCapacity.setEditable(true);
} else {
txtCapacity.setEditable(false);
System.out.println("enter only the numeric number");
}
}
});
它验证了但这一个的问题是我不能使用退格键用于删除号码。请告诉我验证小数的想法。
提前致谢
不要使用KeyListener
! Use a VerifyListener
相反,因为这将处理粘贴、退格、替换......
E.g.
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 {
new BigDecimal(newS);
// value is decimal
} catch (final NumberFormatException numberFormatException) {
// value is not decimal
e.doit = false;
}
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)