【问题标题】:Actionscript 3 TextField scrollH property gets set to 0 on clickActionscript 3 TextField scrollH 属性在单击时设置为 0
【发布时间】:2010-11-29 22:08:01
【问题描述】:

我正在以编程方式填充文本字段,并在添加新文本时滚动到 maxScrollH,以便用户可以看到文本的进度。这工作正常,直到我单击 TextField,它将 scrollH 设置回 0 并将插入符号放在文本中的等效位置。

textField.setSelection( text.length, text.length ); //sets the caretIndex/selection to the end
textField.scrollH = textField.maxScrollH; //scrolls to max

这是我在 textField 文本属性更新时用来滚动的代码。我尝试在 textField 上的 click 事件中添加一个侦听器,它以某种方式工作,但会导致可见的跳转。

override protected function createChildren() : void
{
    super.createChildren();
    textField.addEventListener(MouseEvent.CLICK, handleTextFieldClick, false, 0, true);
}

protected function handleTextFieldClick(event:MouseEvent):void
{
    textField.scrollH = currentTextFieldScrollPosition; //stored scrollH value
    trace(textField.scrollH);
}

我的猜测是有一个滚动位置正在计算或存储在我找不到的地方。

【问题讨论】:

    标签: apache-flex actionscript-3 scroll textfield


    【解决方案1】:

    事实证明,您需要为带有 UIScrollbar 的窗口创建一个 onclick 处理程序,然后为其添加一个焦点输出事件侦听器,将 onclick 时的滚动条位置传递给焦点。 ActionScript 3: maintaining textarea UIscrollbar position on loss of focus in flash embed

    【讨论】:

      【解决方案2】:

      flex TextInput 设置 textField 的选择:

      /**
       *  @private
       *  Gets called by internal field so we draw a focus rect around us.
       */
      override protected function focusInHandler(event:FocusEvent):void
      {
          if (event.target == this)
              systemManager.stage.focus = TextField(textField);
      
          var fm:IFocusManager = focusManager;
      
          if (editable && fm)
          {
              fm.showFocusIndicator = true;
              if (textField.selectable &&
                  _selectionBeginIndex == _selectionEndIndex)
              {
                  textField.setSelection(0, textField.length);
              }
          }
      
          [...]
      

      在我的组件中覆盖它可以解决这个问题:

          override protected function focusInHandler(event:FocusEvent):void
          {
              super.focusInHandler(event);
              textField.scrollH = currentTextFieldScrollPosition;
          }
      

      【讨论】:

      • 我遇到了同样的 AS3 问题(TextArea 中的 UIScrollbar 在获得然后失去对 textarea 的关注时重置其位置),但我似乎根本没有调用 focusInHandler 来覆盖它。我也没有得到第一个代码块——这是 focusInHandler 通常做的吗?你想用它做什么不同的事情? [...] 让我感到困惑——如果它不是 focusinhandler 的默认代码,你为什么要截断它?我对 AS3 比较陌生。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      相关资源
      最近更新 更多