flex4的buttonBar点击取值问题
buttonBar是我用的数据源绑定的方式赋值的,我现在需要处理单击事件.当单击的时候,取到当前按钮的值,传到后台.不知道怎么取.请教各位大侠.
我想要的不是selectedItem,因为用这个的话,点击同一个按钮两次的话,传到后台的值,会有为空的情况.因为那个按钮的状态处于了为被选中的状态.
[解决办法]
你这问法,点击两次,可以进行判断,另外就是获取值,可以看一下例子,应该没有问题的!
这是TourDeFlex中的例子,应该是你出现的问题,你可以做一下参考![code=Flex]
<?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" viewSourceURL="srcview/index.html">
<fx:Style>
@namespace "library://ns.adobe.com/flex/spark";
ButtonBar ToggleButton:upAndSelected,
ButtonBar ToggleButton:overAndSelected,
ButtonBar ToggleButton:downAndSelected,
ButtonBar ToggleButton:disabledAndSelected {
baseColor: #FFFFFF;
color: #323232;
}
ButtonBar {
baseColor: #000000;
color: #FFFFFF;
}
</fx:Style>
<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
/**
* Index change handler will be called each time a button is clicked
* and the event will provide information needed such as the previous
* index clicked.
**/
protected function indexChangeHandler(event:IndexChangeEvent):void
{
myTextArea.text = "Button Bar index clicked = " + event.newIndex
myTextArea.text = myTextArea.text + "\nButton Bar previous index = " + event.oldIndex;
myTextArea.text = myTextArea.text + "\nButton Bar label clicked = " + myButtonBar.selectedItem;
if (myButtonBar.selectedItem=="Red") {
txtColor.setStyle("color","red");
txtColor.text="Red selected!";
}
else if (myButtonBar.selectedItem=="Green") {
txtColor.setStyle("color","green");
txtColor.text="Green selected!";
}
else if (myButtonBar.selectedItem=="Blue") {
txtColor.setStyle("color","blue");
txtColor.text="Blue selected!";
}
else {
txtColor.setStyle("color","yellow");
txtColor.text="Yellow selected!";
}
}
protected function resetButtonBar(event:MouseEvent):void
{
myButtonBar.selectedIndex = -1;
myTextArea.text = "";
}
]]>
</fx:Script>
<!-- Note: A custom panel skin is used for the Tour de Flex samples and is included in the
source tabs for each sample. -->
<s:Panel title="ButtonBar Sample"
width="100%" height="100%"
horizontalCenter="0"
skinClass="skins.TDFPanelSkin">
<s:HGroup left="5" top="5" width="100%" height="100%">
<s:Label width="50%" fontSize="13" color="0x323232" verticalAlign="justify"
text="The ButtonBar component behaves like a series of toggle buttons, where one button remains selected
and only one button in the ButtonBar control can be in the selected state. That means when you select a button in a ButtonBar
control, the button stays in the selected state until you select a different button."/>
<s:VGroup left="10" top="5" color="0x000000" horizontalAlign="center">
<s:ButtonBar id="myButtonBar" change="indexChangeHandler(event)">
<mx:ArrayCollection>
<fx:String>Red </fx:String>
<fx:String>Green </fx:String>
<fx:String>Blue </fx:String>
<fx:String>Yellow </fx:String>
</mx:ArrayCollection>
</s:ButtonBar>
<s:TextArea id="myTextArea" width="{myButtonBar.width}" height="150"/>
<s:Button id="myButton" label="Reset Selection" click="resetButtonBar(event)"/>
<s:Label id="txtColor" fontSize="16"/>
</s:VGroup>
</s:HGroup>
</s:Panel>
</s:Application>
[/code]