【问题标题】:spark combobox dataprovider validator火花组合框数据提供者验证器
【发布时间】:2012-05-03 07:24:58
【问题描述】:

当用户输入的值不是该组合框的数据提供者中的值时,我想对火花组合框进行验证。谁能给我代码,如果用户输入的值不是应进行数据提供者和焦点更改验证。 谢谢

【问题讨论】:

  • 如果你不想让用户填写其他值,为什么不直接使用 DropDownList 呢?
  • 要求它应该是可编辑和可选择的,但下拉列表是不可编辑的,并且当用户第一次按下键盘上的任何字符时它不会被选中,我们应该改变焦点以显示选定的值下拉列表。

标签: actionscript-3 apache-flex actionscript flex4


【解决方案1】:

您可以将属性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)"
             />

【讨论】:

  • 嗨 Marcx 感谢您的回答,但您能告诉我我应该在更改事件处理程序中写什么,以便向用户抛出验证消息。
  • 我不确定我是否正确理解了您的需求,但您可以这样做......(检查我的答案中的编辑)
  • Marcx 再次感谢您的回复。您正确理解我的要求。我使用了您的代码,但现在问题是我也使用了 change='indexChangeHandler(event)';连同 textInput="change(event)" 但现在当没有选择任何内容时,不应调用更改事件,但目前正在调用它。如何停止。如果你告诉我如何用红色边框给出验证错误而不是在警报中显示,这可能会更有帮助。
  • 非常感谢。最后,我不能使用字符串验证器来确定用户输入的值是否在组合框的数据提供者中,以便我可以为字符串验证器提供源。实际上,我有很多动态创建的组合框,因此当用户在组合框中输入错误的值和按下保存按钮所有验证错误都应该出现在字符串验证器中。
猜你喜欢
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多