" event.target as Checkbox"正在传递"选择"的不同值属性通过Mouse CLICK事件

时间:2015-05-05 12:18:58

标签: actionscript-3 flex actionscript flex4

我有一个复选框,我正在设置"已选择"属性编程。设置值后,鼠标单击处理程序开始运行,其余逻辑就在那里。

事件处理程序可以通过以下方式获取选定的Checkbox:

event.target as CheckBox;

但它没有取得"选择"的适当价值。属性。对于例如如果我将属性设置为" FALSE",则在获取CheckBox后,它的值被发现为" TRUE"。 这种行为很奇怪,我之前从未遇到过这样的问题。

非常感谢在这方面的任何帮助!

以下是代码快照:

CheckBox.selected设置为FALSE:

filterUIObject.filterCheckBox.selected = false;
filterUIObject.filterCheckBox.dispatchEvent(new MouseEvent(MouseEvent.CLICK));

filterUIObject是一个Object,它持有对多个控件和容器的引用,如下所示:

var filterUIObject:Object = new Object();               
            var cbSelected:Boolean = false;
            var tiValue:String = "";
            var dateFilterName:String = dateByVariableFilterNames.getItemAt(i) as String;


            var dateByVarCB:CheckBox = new CheckBox();
            dateByVarCB.addEventListener(MouseEvent.CLICK, checkBoxClickHandler);
            dateByVarCB.id = dateFilterName+"_CB"; 
            dateByVarCB.name = dateFilterName;
            dateByVarCB.label = dateFilterName;
            dateByVarCB.toolTip = RB.getString('Common.ToolTip.SelectOccurrences.txt');

            //dateByVarCB.selected = cbSelected;

            // Creates last <TextInput> occurrences
            var lastOccurrencesTI:TextInput = new TextInput();
            lastOccurrencesTI.id = dateFilterName+"_TI";
            lastOccurrencesTI.name = dateFilterName+"_TI";
            lastOccurrencesTI.toolTip = RB.getString('Common.ToolTip.Occurrences.txt');
            lastOccurrencesTI.width = 30
            lastOccurrencesTI.text = ""+tiValue;
            lastOccurrencesTI.restrict = RESTRICT_TO_CHARS;
            lastOccurrencesTI.maxChars = 2;

            var occurrencesLabel:Label = new Label();
            occurrencesLabel.text = RB.getString('GallerySecondaryFilterPage.Occurrences.label.txt');

            var eachRowContainer:GridRow = new GridRow();

            var gridItem:GridItem = new GridItem();

            var filterContainerVB:VBox = new VBox();
            filterContainerVB.id = dateFilterName;
            filterContainerVB.name = dateFilterName;
            filterContainerVB.percentWidth = 33;
            filterContainerVB.height = 65;

            var horizontalContainer:HBox;
            horizontalContainer = new HBox();
            horizontalContainer.percentWidth = 100
            horizontalContainer.percentHeight = 100;

            var spaceVarLabel:Label = new Label();
            spaceVarLabel.text = "";
            horizontalContainer.addChild(spaceVarLabel);
            horizontalContainer.addChild(lastOccurrencesTI);
            horizontalContainer.addChild(occurrencesLabel);

            filterContainerVB.addChild(dateByVarCB);
            filterContainerVB.addChild(horizontalContainer);
            gridItem.addChild(filterContainerVB);

            eachRowContainer.addChild(gridItem);
            this.addChild(eachRowContainer);

            // update data in filterUIObject.
            filterUIObject.filterContainer = eachRowContainer;
            filterUIObject.dateFilterName = dateFilterName;
            filterUIObject.filterCheckBox = dateByVarCB; 
            filterUIObject.filterTextInput = lastOccurrencesTI;                              
            filterUIObject.filterOccurrencesLabel = occurrencesLabel;
            filterUIObjects[i] = filterUIObject;

这是事件处理程序:

    private function checkBoxClickHandler(event:MouseEvent):void
    {
        var selectedCheckBox:CheckBox = event.target as CheckBox;
        var checkBoxID:String = selectedCheckBox.id;

        if(selectedCheckBox.selected)
        {
            for each(var filterUIObject:Object in filterUIObjects)
            {
                if(checkBoxID == filterUIObject.filterCheckBox.id)
                {
                    filterUIObject.filterOccurrencesLabel.enabled = true;
                    filterUIObject.filterTextInput.enabled = true;
                    //filterUIObject.filterTextInput.text = DEFAULT_OCCURENCE_VALUE;
                    break;                      
                }
            }   
        }
        else
        {
            for each(filterUIObject in filterUIObjects)
            {
                if(checkBoxID == filterUIObject.filterCheckBox.id)
                {
                    filterUIObject.filterOccurrencesLabel.enabled = false;
                    filterUIObject.filterTextInput.enabled = false;
                    //filterUIObject.filterTextInput.text = "";
                    break;                      
                }
            }               
        }
    }

每当我调试时,上面事件处理程序中的代码总是会进入&#39; IF&#39;阻止,永远不要进入&#39; ELSE&#39;阻止(即使初始值设置为&#39; FALSE&#39;)。

是因为我将控件引用存储在Object中吗?如果是,可以采取哪些措施来检索正确的值?

1 个答案:

答案 0 :(得分:1)

你可以尝试使用spark复选框而不是mx复选框,如果你的组件是火花你的代码将起作用。 要修复mx(Halo主题),您可以使用更改事件而不是鼠标单击事件尝试此代码

filterUIObject.filterCheckBox.selected = false;    
filterUIObject.filterCheckBox.dispatchEvent(new Event(Event.CHANGE));  
private function checkBoxClickHandler(event:Event):void
{
}