0

一些浏览器为 HTML 文本区域显示一个句柄来调整文本框的大小。有什么方法可以对 GWT 中的这种调整大小事件做出反应?

我用这段代码试过了,但没有触发事件:

TextArea textArea = new TextArea();

textArea.addHandler(new ResizeHandler() {

    @Override
    public void onResize(ResizeEvent event) {
        System.out.println("resize");

    }
}, ResizeEvent.getType());
4

2 回答 2

1

“看起来您不能专门将事件附加到调整文本区域的大小。调整窗口大小时会触发 resize 事件。

https://stackoverflow.com/a/2096352/1467482

于 2013-08-13T21:21:44.477 回答
0

这个问题已经有两年的历史了,但对于被谷歌带到这个问题的人来说,我有一个解决方案。

您可以使用 mouse-down、mouse-move 和 mouse-up 事件来对文本区域的大小调整做出反应。这是一个骨架代码:

TextArea textArea = new TextArea();
...
textArea.addMouseDownHandler(new MouseDownHandler() {
  private HandlerRegistration mouseMoveUpRegistration;
  private int lastWidth;
  private int lastHeight;

  @Override
  public void onMouseDown(MouseDownEvent event) {
    lastWidth = getOffsetWidth();
    lastHeight = getOffsetHeight();

    if (mouseMoveUpRegistration == null) {
      mouseMoveUpRegistration = Event.addNativePreviewHandler(new NativePreviewHandler() {
        @Override
        public void onPreviewNativeEvent(NativePreviewEvent event) {
          if (event.getTypeInt() == Event.ONMOUSEMOVE || event.getTypeInt() == Event.ONMOUSEUP) {
            int width = getOffsetWidth();
            int height = getOffsetHeight();
            if (width != lastWidth || height != lastHeight) {
              // do whatever you want to to while still resizing
              lastWidth = width;
              lastHeight = height;
            }

            if (event.getTypeInt() == Event.ONMOUSEUP) {
              // do whatever you want to do when resizing finished
              // perhaps check actual and initial size to see if the size really changed
              /* mouse up => additionally remove the handler */
              if (mouseMoveUpRegistration != null) {
                mouseMoveUpRegistration.removeHandler();
                mouseMoveUpRegistration = null;
              }
            }
          }

      });
    }
  }

});

于 2016-01-28T18:04:48.077 回答