GWT:检查事件处理程序是否存在

发布于 2024-11-05 21:35:01 字数 660 浏览 0 评论 0原文

我正在使用按键处理程序根据文本框的字符串值添加和删除事件处理程序。我不想在每个按键上添加或删除事件处理程序。我如何首先检查处理程序是否已存在?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2024-11-12 21:35:01

if (firstHandler != null) 将完成这项工作,当您删除处理程序时,将其注册设为 null:

firstHandler.removeHandler();
firstHandler = null;

if (firstHandler != null) will do the job, and when you remove handler, null it registration:

firstHandler.removeHandler();
firstHandler = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文