有没有办法检测键盘上的退格键何时被按下,使用文档过滤器? The following is an edited code extract from here检测退格键按
对于实施例
public class IntFilter extends DocumentFilter {
boolean trueFalse = true;
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String string, AttributeSet attr)
throws BadLocationException {
StringBuffer buffer = new StringBuffer(string);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch)) {
buffer.deleteCharAt(i);
trueFalse = false;
}
/*
else if (backspace pressed)
{
trueFalse = true;
}
*/
else{
trueFalse = true;
}
}
super.insertString(fb, offset, buffer.toString(), attr);
}
public void replace(DocumentFilter.FilterBypass fb,
int offset, int length, String string, AttributeSet attr) throws BadLocationException {
if (length > 0) fb.remove(offset, length);
insertString(fb, offset, string, attr);
}
}
使用文档过滤器是绝对必要的吗? – nom
@NabeelOmer对于这个问题是的。在实际的程序中,我正在试验一个DocumentListener – Dan