spark combobox dataprovider验证器

时间:2012-05-03 07:24:58

标签: actionscript-3 flex actionscript flex4

当用户输入除组合框的数据提供者中的值以外的值时,我想对spark组合框进行验证。任何人都可以给我代码如果用户输入的值不是应该进行dataprovider和焦点更改验证。 感谢

1 个答案:

答案 0 :(得分:0)

您可以将属性textInput设置为组合框,并管理相关函数的输入检查......

<fx:Script>
    <![CDATA[
        protected function change(event:TextEvent):void
        {

        }
    ]]>
</fx:Script>

<s:ComboBox  textInput="change(event)"/>
  

flash.display.InteractiveObject.textInput

     

当用户输入一个或多个文本字符时调度。各种文本输入方法都可以   生成此事件,包括标准键盘,输入法   编辑(IME),语音或语音识别系统,甚至是行为   粘贴没有格式或样式信息的纯文本。

     

活动类型:
  flash.events.TextEvent.TEXT_INPUT

修改

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        //dataprovider initialization
        [Bindable]private var d:ArrayCollection = new ArrayCollection([
            {name: "Values 1"}, {name: "Values 2"}, {name: "Values 3"}
            ]);

        protected function change(event:TextEvent):void
        {      // if enter key is pressed
            if (event.text.charAt(event.text.length-1) == "\r")
            {       
                // if the text inserted in the combobox is one of the 
                // item in the dataprovider 
                if (comboBox.selectedIndex >= 0)
                    Alert.show("something selected");
                else   // if the text is not an item in the dataprovider
                    Alert.show("nothing selected");

            }
        }
    ]]>
</fx:Script>

<s:ComboBox id="comboBox" textInput="change(event)"
       dataProvider="{d}" labelField="name"/>

编辑2

要设置动作脚本的边框颜色,您可以这样做:

comboBox.setStyle("borderColor","#ff0000"); // set the bordercolor to red

如果您不需要对插入的文本进行特殊检查,只需在ComboBox上设置属性更改

        protected function index_change(event:IndexChangeEvent):void
        {
            if (comboBox.selectedIndex >= 0 )
                Alert.show("something selected")
            else
                Alert.show("nothing selected");

        }
        <s:ComboBox id="comboBox" dataProvider="{d}" labelField="name"
            change="index_change(event)"
             />