Actionscript 3 我怎样才能设置;maxChars";文本输入在可编辑组合框中的位置?
我想设置可编辑组合框的TextInput的Actionscript 3 我怎样才能设置;maxChars";文本输入在可编辑组合框中的位置?,actionscript-3,apache-flex,combobox,textinput,Actionscript 3,Apache Flex,Combobox,Textinput,我想设置可编辑组合框的TextInput的maxChars属性。我目前正在使用更改事件将文本修剪为一组字符: private function nameOptionSelector_changeHandler(event:ListEvent):void { nameOptionSelector.text = nameOptionSelector.text.substr(0, MAX_LENGTH); } 这感觉太过分了。必须有更好的方法来实现这一点……您可以扩展组合框,并覆盖内部文本输
maxChars
属性。我目前正在使用更改事件将文本修剪为一组字符:
private function nameOptionSelector_changeHandler(event:ListEvent):void
{
nameOptionSelector.text = nameOptionSelector.text.substr(0, MAX_LENGTH);
}
这感觉太过分了。必须有更好的方法来实现这一点……您可以扩展
组合框
,并覆盖内部文本输入
的默认maxChars
值。如果需要动态设置,可以添加公共方法来设置扩展类的属性。可以扩展组合框
,并覆盖内部文本输入
的默认maxChars
值。如果需要动态设置,可以添加一个公共方法来设置扩展类的属性。根据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;
}
}
}
根据Stigler的建议,以下是我实施的完整解决方案:
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;
}
}
}
我的选择是直接使用受保护的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;
}
}
}
我的选择是直接使用受保护的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;
}
}
}
这是我最初的想法,结果证明是一个非常好的主意。下面是完整的解决方案……这是我最初的想法,结果证明是一个非常好的主意。下面是完整的解决方案…当我试图编译这个时,我得到了一个“不兼容的覆盖”,可能是因为
公共函数itemToLabel(item:Object,…rest):String
原型。我真的不知道用这些参数做什么(我猜是什么都做不了)。您使用的是什么SDK?我使用的是3.5,当我试图编译它时,我得到了一个“不兼容的覆盖”,可能是因为公共函数itemToLabel(item:Object,…rest):String
原型。我真的不知道用这些参数做什么(我猜是什么都做不了)。您使用的是什么SDK?我用的是3.5