GWT TextArea - 调整大小处理程序

时间:2013-08-13 21:06:35

标签: java javascript html gwt

有些浏览器会为HTML Textareas显示调整文本框大小的句柄。有没有办法对GWT中的调整大小事件做出反应?

我尝试使用此代码,但事件未触发:

TextArea textArea = new TextArea();

textArea.addHandler(new ResizeHandler() {

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

    }
}, ResizeEvent.getType());

2 个答案:

答案 0 :(得分:1)

“看起来你不能专门附加事件来调整textarea的大小。调整窗口大小时会调整resize事件。

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

答案 1 :(得分:0)

这个问题已经有两年了,但是对于那些由谷歌带来这个问题的人我有一个解决方案。

您可以使用鼠标按下,鼠标移动和鼠标移动事件来响应文本区域的大小调整。这是一个骨架代码:

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;
              }
            }
          }

      });
    }
  }

});