如何以编程方式将 RadioButton 与 ActionScript3 中的 RadioButtonGroup 关联起来?
我有一个 UI 组件,由于各种原因,我必须以编程方式构建它。 该组件是一个按列分组的单选按钮表。
现在,我正在构建这样的列组:
private function createGroupsForItemList(items: XMLList): void {
for each (var item: XML in items) {
var rbGroup: RadioButtonGroup = new RadioButtonGroup();
groups[[email protected]()] = rbGroup;
}
}
我正在尝试将 RadioButton
实例与列组关联起来,如下所示:
private function createValueControl(item: XML): UIComponent {
var control: RadioButton = new RadioButton();
control.label = "";
control.group = groups[[email protected]()];
control.addEventListener(Event.SELECT, updateSelection);
return control;
}
我可以在调试器中看到该控件与group:
control.group == groups[[email protected]()]
但是,我同样可以看到该组对控件一无所知:
group.radioButtons.length == 0
我想这是因为 RadioButton
中 group
的 setter 是一个愚蠢的 setter; 它所做的只是复制到变量,这并不能发挥 groupName
的魔力。 但是,我似乎找不到应该用来正确设置 RadioButton.groupName
属性的值。
所以,简而言之,我对如何让这些部分相互对话感到困惑。 我该怎么做呢?
-- 编辑 --
事实证明,我可以通过设置 groupName 属性来创建和关联组,但我无法在组中设置选择侦听器; 设置过程结束后,该组立即为 NULL,这意味着下面的第二行抛出 NPE 的 Flex 等价项:
control.groupName = groupNameForLevel(item);
control.group.addEventListener(Event.SELECT, updateSelection);
I have a UI component that, for various reasons, I have to construct programatically. The component is a table of radio buttons grouped by column.
Right now, I'm constructing the column groups like so:
private function createGroupsForItemList(items: XMLList): void {
for each (var item: XML in items) {
var rbGroup: RadioButtonGroup = new RadioButtonGroup();
groups[[email protected]()] = rbGroup;
}
}
I'm trying to associate the RadioButton
instances with the column groups like so:
private function createValueControl(item: XML): UIComponent {
var control: RadioButton = new RadioButton();
control.label = "";
control.group = groups[[email protected]()];
control.addEventListener(Event.SELECT, updateSelection);
return control;
}
I can see in the debugger that the control has an association to the group:
control.group == groups[[email protected]()]
However, I can see equally that the group does not know anything about the control:
group.radioButtons.length == 0
I imagine that this is because the setter for group
in RadioButton
is a dumb setter; all it does is copy to the variable, which doesn't do the magic that groupName
does. However, I can't seem to find the value I should use to set the RadioButton.groupName
property correctly.
So, in short, I'm stumped on how to get these bits to talk to each other. How do I do this?
-- EDIT --
It turns out that I can have the groups created and associated simply by setting the groupName property, but I can't get at the group to set up a selection listener; the group is NULL immediately after the setting process, which means that the second line below throws the Flex equivalent of an NPE:
control.groupName = groupNameForLevel(item);
control.group.addEventListener(Event.SELECT, updateSelection);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置 groupName 应该可以。
我所能建议的就是单步执行 RadioButton 组件的 group() getter 并查看它到底在哪里失败。 您也在以编程方式创建组吗? 如果是这种情况,可能还没有完全初始化。
Setting groupName should work.
All I can suggest is to step through the group() getter of the RadioButton component and see where exactly it is failing. Are you programmatically creating the group too? If that's the case, maybe it isn't initialized fully yet.
第一直觉是这个问题与 invalidateDisplayList 以及它的调用时间和方式有关。 当然,由于与该功能相关的问题是 Flex 的许多怪癖背后的原因,我可能只是在寻找替罪羊。
这本身并不是您问题的答案,但它似乎实际上可以作为替代解决方案。
RadioButtonGroups 将基于 IFlexDisplayObject 进行初始化。 这意味着您可以执行以下操作:
问题是它可能不是最实用的答案,但它作为可行的解决方案具有明显的好处。
First instinct is that this issue has to do with invalidateDisplayList and when and how that is called. Of course, since issues related to that function are behind a number of Flex's quirks, I may just be scapegoating.
This is not the answer to your question per se, but it seems like it might actually work as an alternate solution.
RadioButtonGroups will initialize based on a IFlexDisplayObject. This means that you can do something like:
The problem is that it may not be the most practical answer, but it has the decided benefit of being a workable solution.