1

有一个 ComboBox 和一个数据提供者,其中有 n>3 个字符串值。

combobox.inputText.text = "value in dataprovider";
// OK, selectedIndex is set also

几秒钟后,由用户按下按钮启动:

combobox.selectedIndex = 3; 
// OK

几秒钟后再次这样做

combobox.inputText.text = "value NOT in dataprovider";

最后一行设置了 combobox.inputText,但让 selectedIndex 为 3,尽管 inputText-value 不在 dataprovider 值中。

这可以通过按下按钮 1,然后按下按钮 4,然后再按下按钮 1 来通过以下示例来证明。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               initialize="initializeHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            [Bindable] private var array : ArrayCollection;

            protected function initializeHandler(event:FlexEvent):void {
                array = new ArrayCollection();
                array.addItem("0:00");
                array.addItem("0:30");
                array.addItem("1:00");
                array.addItem("1:30");
                addEventListener(Event.ENTER_FRAME, ef);
            }

            protected function btnSelect1_clickHandler(event:MouseEvent):void {
                cb.selectedIndex = 3;
            }

            protected function btnSelect2_clickHandler(event:MouseEvent):void {
                cb.selectedIndex = -1;
            }

            protected function btnSelect3_clickHandler(event:MouseEvent):void {
                cb.textInput.text = "1:00";
            }

            protected function btnSelect4_clickHandler(event:MouseEvent):void {
                cb.textInput.text = "1:01";
            }

            protected function ef(event:Event):void {
                l.text = "inputText=\"" + cb.textInput.text + "\" selectedIndex=\""+cb.selectedIndex+"\"";
            }

        ]]>
    </fx:Script>
    <s:VGroup>
        <s:ComboBox id="cb" dataProvider="{array}"/>
        <s:Button label="select index 3" click="btnSelect1_clickHandler(event)" /> 
        <s:Button label="select index -1" click="btnSelect2_clickHandler(event)" />
        <s:Button label="select '1:00'" click="btnSelect3_clickHandler(event)" /> 
        <s:Button label="select '1:01'" click="btnSelect4_clickHandler(event)" />
        <s:Label id="l" />
    </s:VGroup>
</s:Application>
4

1 回答 1

1

尽量不要直接操作inputText. 相反,使用该方法getItemIndexArrayCollection. 请参阅上面重写的函数:

protected function btnSelect3_clickHandler(event:MouseEvent):void {
    cb.selectedIndex = array.getItemIndex("1:00");
}

protected function btnSelect4_clickHandler(event:MouseEvent):void {
    cb.selectedIndex = array.getItemIndex("1:01");
}
于 2012-12-18T16:25:21.940 回答