GWT - 无法将文本输入到 TextArea

GWT - Not able to input text into TextArea

我有一个奇怪的问题要解决。 我有一个无法向其输入文本的 GWT Textarea,但是,我可以自动设置文本。

我已经试过了

textBox.setEnabled(true);
textBox.setFocus(true);
textBox.setReadOnly(false);

但它并没有改变任何东西。 通过 css 文件时,我觉得一切正常。

这里是 "whole" 源代码:

private Input(AbsolutePanel canvas) {

    textBox = new MouseEventTextBox();
    textBox.setStylePrimaryName(PRIMARY_STYLE);
    textBox.addKeyUpHandler(this);
    textBox.addKeyDownHandler(this);
    textBox.addFocusHandler(this);
    textBox.addBlurHandler(this);
    textBox.setEnabled(true);
    textBox.setFocus(true);
    textBox.setReadOnly(false);
    textBox.setText("Just an apple"); // Works, text is set but not editable
    canvas.add(textBox, -1000, -1000);
}

private class MouseEventTextBox extends TextArea{
    public MouseEventTextBox() {
        super();
        sinkEvents(Event.MOUSEEVENTS);
    }

    public void onBrowserEvent(Event event) {
        // Call the superclass' implementation first.
        super.onBrowserEvent(event);

        if ((DOM.eventGetButton(event) == Event.BUTTON_LEFT) && (DOM.eventGetType(event) == Event.ONMOUSEUP)) {
            this.setReadOnly(false);
            DOM.eventCancelBubble(event, true);
        }
    }
}

CSS

border-width: 0px;
background-color: #fffde5;
padding-top: 2px;
padding-left: 3px;
padding-right: 3px;
padding-bottom: 1px;
z-index: 50;
overflow: hidden;

也许有人可以给我提示或知道这里出了什么问题?

非常感谢!

您代码中可疑的部分是:

textBox.addKeyUpHandler(this);
textBox.addKeyDownHandler(this);
textBox.addFocusHandler(this);
textBox.addBlurHandler(this);

这意味着,this 实现了 KeyUpHandlerKeyDownHandlerFocusHandlerBlurHandler。不幸的是,您没有向我们展示处理这些事件的方法。

我敢打赌,您的 onKeyDown 方法有问题。当我阻止 KeyDownEvent 事件(stopPropagationpreventDefault)时,我能够实现相同的行为。

检查您的浏览器控制台是否有错误。如果none,注释掉textBox.addKeyDownHandler(this);行。