【问题标题】:How can I set "maxChars" of the TextInput in an editable ComboBox?如何在可编辑的 ComboBox 中设置 TextInput 的“maxChars”?
【发布时间】:2010-10-25 08:52:00
【问题描述】:

我想设置可编辑 ComboBox 的 TextInput 的 maxChars 属性。我目前正在使用更改事件将文本修剪为一定数量的字符:

private function nameOptionSelector_changeHandler(event:ListEvent):void
{
    nameOptionSelector.text = nameOptionSelector.text.substr(0, MAX_LENGTH);
}

这感觉有点矫枉过正。必须有更好的方法来做到这一点......

【问题讨论】:

    标签: actionscript-3 apache-flex combobox textinput


    【解决方案1】:

    我的替代方法是直接使用受保护的 textInput。这种方法允许在 GUI 构建器或代码中设置“maxChars”属性,就像对普通 TextField 所做的那样。请注意,零是 maxChars 的有效值,表示字符数不限。需要覆盖 .childrenCreated() 以避免在 TextInput 对象存在之前尝试设置 maxChars。

    package my.controls
    {
        import mx.controls.ComboBox;
    
        public class EditableComboBox extends ComboBox
        {
            public function EditableComboBox()
            {
                super();
            }
    
            private var _maxChars:int = 0;
    
            override protected function childrenCreated():void
            {
                super.childrenCreated();
    
                // Now set the maxChars property on the textInput field.
                textInput.maxChars = _maxChars;
            }
    
            public function set maxChars(value:int):void 
            {
                _maxChars = value;
                if (textInput != null && value >= 0)
                    textInput.maxChars = value;
            }
    
            public function get maxChars():int 
            {
                return textInput.maxChars;
            }
    
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以扩展ComboBox 并覆盖内部TextInput 的默认maxChars 值。如果需要动态设置,可以添加公共方法来设置扩展类的属性。

      【讨论】:

      • 这是我最初的想法,结果证明是一个非常好的想法。下面的完整解决方案...
      【解决方案3】:

      使用 Stiggler 的建议,这是我实施的完整解决方案:

      package
      {
          import mx.controls.ComboBox;
      
          public class ComboBoxWithMaxChars extends ComboBox
          {
              public function ComboBoxWithMaxChars()
              {
                  super();
              }
      
              private var _maxCharsForTextInput:int;
      
              public function set maxCharsForTextInput(value:int):void
              {
                  _maxCharsForTextInput = value;
      
                  if (super.textInput != null && _maxCharsForTextInput > 0)
                      super.textInput.maxChars = _maxCharsForTextInput;
              }
      
              public function get maxCharsForTextInput():int
              {
                  return _maxCharsForTextInput;
              }
      
              override public function itemToLabel(item:Object):String
              {
                  var label:String = super.itemToLabel(item);
      
                  if (_maxCharsForTextInput > 0)
                      label = label.substr(0, _maxCharsForTextInput);
      
                  return label;
              }
          }
      }
      

      【讨论】:

      • 当我试图编译这个时,我得到了一个“不兼容的覆盖”,可能是因为 public function itemToLabel(item:Object, ... rest):String 原型。我真的不知道用这些参数做什么(我的猜测是没有什么可做的)。你用的是什么SDK?我正在使用 3.5
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 2010-09-10
      • 1970-01-01
      • 2013-01-12
      相关资源
      最近更新 更多