处理 GWT-Ext NumberField 中 ENTER 键的回调

发布于 2024-08-07 13:12:13 字数 644 浏览 7 评论 0原文

我使用 GWT-Ext 库为 Web 应用程序构建 GUI。 我想处理 NumberField 内的 ENTER 按键。

应该是这样的:

    final NumberField requiredHeight = new NumberField();
    requiredHeight.setValue(125);
    requiredHeight.setAllowBlank(false);
    requiredHeight.setAllowNegative(false);
    requiredHeight.setAllowDecimals(false);
    KeyListener listener = new KeyListener() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode()==13)
                addPortlet(requiredHeight.getValue().intValue());
        }

    };
    requiredHeight.addKeyListener(listener);

但是代码没有做任何事情! 我做错了什么?一般来说,将这样的处理程序添加到字段的最佳方法是什么?

I use GWT-Ext library for building GUI for a webapp.
I'd like to process an ENTER key press inside of the NumberField.

It should be like this:

    final NumberField requiredHeight = new NumberField();
    requiredHeight.setValue(125);
    requiredHeight.setAllowBlank(false);
    requiredHeight.setAllowNegative(false);
    requiredHeight.setAllowDecimals(false);
    KeyListener listener = new KeyListener() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode()==13)
                addPortlet(requiredHeight.getValue().intValue());
        }

    };
    requiredHeight.addKeyListener(listener);

But the code doesn't do anything!
What am I doing wrong? Generally speaking, what is the best way to add such a handler to the field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

清醇 2024-08-14 13:12:13

好吧,终于解决了。 KeyListener 内部还有另一个回调函数 - componentKeyPress 而不是 keyPressed。这是正确的代码:

    KeyListener listener = new KeyListener() {
        @Override
        public void componentKeyPress(ComponentEvent event) {
            if(event.getKeyCode()==13)
            {
                addPortlet(requiredHeight.getValue().intValue());
            }
            super.componentKeyPress(event);
        }

    };
    requiredHeight.addKeyListener(listener);

OK, solved that at last. There is another callback function inside of KeyListener - componentKeyPress instead of keyPressed. This is the correct code:

    KeyListener listener = new KeyListener() {
        @Override
        public void componentKeyPress(ComponentEvent event) {
            if(event.getKeyCode()==13)
            {
                addPortlet(requiredHeight.getValue().intValue());
            }
            super.componentKeyPress(event);
        }

    };
    requiredHeight.addKeyListener(listener);
酒绊 2024-08-14 13:12:13

有些方法已被弃用,我在简单的登录表单上使用以下代码:

    KeyDownHandler handler = new KeyDownHandler() {

        @Override
        public void onKeyDown(KeyDownEvent event) {
            if (event.getNativeKeyCode() == 13) {
                btnLogin.click(); 
            }
        }
    };
    tbLogin.addKeyDownHandler(handler);
    tbPassword.addKeyDownHandler(handler);

Some methods are deprecated, I use following code on my simple login form:

    KeyDownHandler handler = new KeyDownHandler() {

        @Override
        public void onKeyDown(KeyDownEvent event) {
            if (event.getNativeKeyCode() == 13) {
                btnLogin.click(); 
            }
        }
    };
    tbLogin.addKeyDownHandler(handler);
    tbPassword.addKeyDownHandler(handler);
冷弦 2024-08-14 13:12:13

注册一个按键侦听器实际上会将侦听器注册到 3 个不同的事件:按键、按键和按键,其中两个对于您的情况来说是完全没有意义的。

更紧凑的解决方案是使用通用侦听器,如下所示:
(请原谅我使用匿名侦听器类,它更快但显然具有相同的效果)

import com.extjs.gxt.ui.client.event.Events; 

requiredHeight.addListener(Events.OnKeyPress, new Listener<ComponentEvent>() {
    @Override
    public void handleEvent(ComponentEvent e) {
        if (e.getKeyCode()==13)
            addPortlet(requiredHeight.getValue().intValue());           
    }
});

Registering a key listener in fact registers the listener to 3 different events: key-up, key-down and key-press, two of which are completely pointless in your case.

A more compact solution would be the usage of a generic listener, as follows:
(pardon me for using an anonymous listener class, it is quicker yet evidently has the same effect)

import com.extjs.gxt.ui.client.event.Events; 

requiredHeight.addListener(Events.OnKeyPress, new Listener<ComponentEvent>() {
    @Override
    public void handleEvent(ComponentEvent e) {
        if (e.getKeyCode()==13)
            addPortlet(requiredHeight.getValue().intValue());           
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文