取消GWT中的按键事件 [英] Cancel a Keypress Event in GWT
问题描述
我想取消一个长文本框的按键事件,以便用户新按下的字符不会输入到文本框中。
I want to cancel a keypress event for a long textbox so that the character newly pressed by the user is not entered in the textbox
longBox_1.addKeyPressHandler(new KeyPressHandler(){
@Override
public void onKeyPress(KeyPressEvent event) {
String Valid="1234567890";
if (!Valid.contains(String.valueOf(event.getCharCode()))) {
// the code to cancel the event to be placed here
}
}
});
推荐答案
如果您的longBox_1是您班级的私人成员,或者是一个final var,取消事件的代码是:
If your longBox_1 is a private member of your class, or a final var, the code to cancel the event is :
longBox_1.cancelKey();
否则,您可以投射事件的来源,如果您确定它对应于TextBox:< /()>
Otherwise you can cast the source of the event, if you are sure it correspond to the TextBox :
((TextBox)event.getSource()).cancelKey();
以下是cancelKey的文档:
Here is the doc for cancelKey :
如果一个键盘事件当前在这个文本框中处理
,调用这个
方法将会抑制它。这允许
监听器可以轻松地过滤键盘
输入
If a keyboard event is currently being handled on this text box, calling this method will suppress it. This allows listeners to easily filter keyboard input
这篇关于取消GWT中的按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!