有沒有辦法檢測鍵盤上的退格鍵何時被按下,使用文檔過濾器? 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