我有这段代码,当按下另一个数字键时会发出一条消息:
txtValueOfClause.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
char ar[] = t.getCharacter().toCharArray();
char ch = ar[t.getCharacter().toCharArray().length - 1];
if (!(ch >= '0' && ch <= '9')) {
System.out.println("The char you entered is not a number");
t.consume();
}
}
});
现在,如果我按了错误的按钮并按退格键将其删除,我也会收到此错误消息。 如何在 if 语句中添加退格输入?
最佳答案
我找到了答案:
txtValueOfClause.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
char ar[] = t.getCharacter().toCharArray();
char ch = ar[t.getCharacter().toCharArray().length - 1];
int codeBackSpace = ch;
if (!(ch >= '0' && ch <= '9') && codeBackSpace!=8) {
System.out.println("The char you entered is not a number");
t.consume();
}
}
});
关于java - 如何过滤退格键盘输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29950364/