"event.target as Checkbox" 正在通过鼠标点击事件传递 "selected" 属性 的不同值
"event.target as Checkbox" is passing different value of "selected" property through Mouse CLICK event
我有一个复选框,我正在为其设置 "selected" 属性 以编程方式。设置值后,鼠标单击处理程序开始运行,其余逻辑就在那里。
事件处理程序能够通过以下方式获取选定的复选框:
event.target as CheckBox;
但它没有获取 "selected" 属性 的正确值。例如如果我将 属性 设置为 "FALSE",那么在获取 CheckBox 之后,它的值被发现是 "TRUE"。
这种行为很奇怪,我以前从未遇到过这样的问题。
在这方面的任何帮助将不胜感激!
代码截图如下:
CheckBox.selected 设置为假:
filterUIObject.filterCheckBox.selected = false;
filterUIObject.filterCheckBox.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
filterUIObject 是一个对象,它持有对多个控件和容器的引用,如下所示:
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;
}
}
}
}
每当我调试时,上面事件处理程序中的代码总是进入 'IF' 块,永远不会进入 'ELSE' 块(即使初始值设置为 'FALSE') .
是否因为我将控件引用存储在对象中?如果是,如何检索正确的值?
您可以尝试使用 spark checkbox 而不是 mx checkbox,如果您的组件是 spark,您的代码就可以工作。
要修复 mx(Halo 主题),您可以使用更改事件而不是鼠标单击事件尝试此代码
filterUIObject.filterCheckBox.selected = false;
filterUIObject.filterCheckBox.dispatchEvent(new Event(Event.CHANGE));
private function checkBoxClickHandler(event:Event):void
{
}
我有一个复选框,我正在为其设置 "selected" 属性 以编程方式。设置值后,鼠标单击处理程序开始运行,其余逻辑就在那里。
事件处理程序能够通过以下方式获取选定的复选框:
event.target as CheckBox;
但它没有获取 "selected" 属性 的正确值。例如如果我将 属性 设置为 "FALSE",那么在获取 CheckBox 之后,它的值被发现是 "TRUE"。 这种行为很奇怪,我以前从未遇到过这样的问题。
在这方面的任何帮助将不胜感激!
代码截图如下:
CheckBox.selected 设置为假:
filterUIObject.filterCheckBox.selected = false;
filterUIObject.filterCheckBox.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
filterUIObject 是一个对象,它持有对多个控件和容器的引用,如下所示:
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;
}
}
}
}
每当我调试时,上面事件处理程序中的代码总是进入 'IF' 块,永远不会进入 'ELSE' 块(即使初始值设置为 'FALSE') .
是否因为我将控件引用存储在对象中?如果是,如何检索正确的值?
您可以尝试使用 spark checkbox 而不是 mx checkbox,如果您的组件是 spark,您的代码就可以工作。 要修复 mx(Halo 主题),您可以使用更改事件而不是鼠标单击事件尝试此代码
filterUIObject.filterCheckBox.selected = false;
filterUIObject.filterCheckBox.dispatchEvent(new Event(Event.CHANGE));
private function checkBoxClickHandler(event:Event):void
{
}