![](/img/trans.png)
[英]A Better Way To Make SelectedIndex Correspond To Selected Item In Flex ComboBox
[英]flex 4 combobox selectedIndex failure
有一个带有数据提供程序的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,但是尽管inputText值不在dataprovider值中,但将selectedIndex设置为3。
通过以下示例可以证明这一点,方法是先按按钮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>
尝试不要直接操纵inputText
。 而是使用getItemIndex
方法在ArrayCollection
查找对象。 请参阅上面重写的函数:
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.