GWT:检查事件处理程序是否存在
我正在使用按键处理程序根据文本框的字符串值添加和删除事件处理程序。我不想在每个按键上添加或删除事件处理程序。我如何首先检查处理程序是否已存在?
HandlerRegistration firstHandler = null;
HandlerRegistration secondHandler = null;
public void onKeyUp(KeyUpEvent event) {
if (countSpaceChar(textBox.getText()) == 0) {
// code to check if MyFirstHandler is already attached?
firstHandler = textBox.addKeyUpHandler(new MyFirstHandler(this));
} if (countSpaceChar(textBox.getText()) == 1) {
firstHandler.removeHandler();
// code to check if MySecondHandler is already attached?
secondHandler = textBox.addKeyUpHandler(new MySecondHandler(this));
}
}
I'm using a key up handler to add and remove event handlers depending on the string value of a text box. I wouldn't want to add or remove an event handler on every key up. How do I first check if a handler already exists?
HandlerRegistration firstHandler = null;
HandlerRegistration secondHandler = null;
public void onKeyUp(KeyUpEvent event) {
if (countSpaceChar(textBox.getText()) == 0) {
// code to check if MyFirstHandler is already attached?
firstHandler = textBox.addKeyUpHandler(new MyFirstHandler(this));
} if (countSpaceChar(textBox.getText()) == 1) {
firstHandler.removeHandler();
// code to check if MySecondHandler is already attached?
secondHandler = textBox.addKeyUpHandler(new MySecondHandler(this));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
if (firstHandler != null)
将完成这项工作,当您删除处理程序时,将其注册设为 null:if (firstHandler != null)
will do the job, and when you remove handler, null it registration: