如何设置“maxChars” 可编辑组合框中的 TextInput?

发布于 2024-07-19 06:56:14 字数 305 浏览 7 评论 0 原文

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

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

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

I want to set the maxChars property of the TextInput of an editable ComboBox. I am currently trimming the text to a set number of characters using a change Event:

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

This feels like overkill. There's got to be a better way to do this....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被你宠の有点坏 2024-07-26 06:56:14

我的替代方法是直接使用受保护的textInput。 这种方法允许在 GUI 构建器或代码中设置“maxChars”属性,就像对普通文本字段所做的那样。 请注意,零是 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;
        }

  }
}

My alternative is to use the protected textInput directly. This approach allows the "maxChars" property to be set in the GUI builder or code, just as you would for a normal TextField. Note that zero is a valid value for maxChars and indicates unlimited characters. The override of .childrenCreated() is needed to avoid attempting to set maxChars before the TextInput object exists.

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

  }
}
下壹個目標 2024-07-26 06:56:14

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

You could extend ComboBox and override the default maxChars value for the internal TextInput. If you need to set it dynamically, you could add a public method to set the property on the extended class.

飘落散花 2024-07-26 06:56:14

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

Using the suggestion of Stiggler, here is the full solution I implemented:

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文