我已经使用名为" addButton()"的函数实际生成了父(标签)对象及其子对象(按钮)。在adobe flex。现在我想调用名为" lable()"的函数。在父对象上,想要调用名为" btn()"的函数在儿童对象上。但是每次点击我的脚本都会调用相同的函数" lable()"。
<?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"
minWidth="127" minHeight="34" backgroundColor="#F4E8E8">
<s:layout>
<s:FormItemLayout/>
</s:layout>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import spark.components.Button;
import mx.controls.Alert;
public function addButton():void {
//Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label="X";
myButton.width = 20;
myButton.height = 20;
myButton.depth =1;
myButton.x=105;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Parent Object
var lble:Label = new Label();
lble.width = 127;
lble.height = 34;
lble.depth =0;
lble.setStyle("backgroundColor",'red');
lble.addEventListener(MouseEvent.CLICK, lable);
lble.addChild(myButton);
myGroup.addElement(lble);
}
private function btn(e:Event):void {
jj.text = 'Text For Button';
}
private function lable(e:Event):void {
kk.text = "Text For Label";
}
]]>
</fx:Script>
<s:HGroup id="myGroup">
<s:Button width="126" height="34" click="addButton();" label="Click" skinClass="spark.skins.spark.ButtonSkin"/>
</s:HGroup>
<s:Label id="jj" x="14" y="150" width="1200" height="50" backgroundColor="gray" text="Button"/>
<s:Label id="kk" x="14" y="69" width="1200" height="50" backgroundColor="gray" text="Label"/>
</s:Application>
请帮帮我
答案 0 :(得分:1)
由于您的按钮位于Label内部,因此两个侦听器都会被调用。只需调用stopPropagation
:
private function btn(e:Event):void {
e.stopPropagation();
jj.text = 'Text For Button';
}
答案 1 :(得分:1)
您可以尝试以下代码来获取类似的行为,而不是在Label中添加Button。
<fx:Script>
<![CDATA[
import mx.graphics.SolidColor;
import spark.components.Button;
import spark.components.Group;
import spark.primitives.Rect;
public function addButton():void {
//Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label="X";
myButton.width = 40;
myButton.height = 20;
myButton.depth =1;
myButton.x=50;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Parent Object
var lble:Group = new Group();
var solidColor:SolidColor = new SolidColor(0xFF0000);
var rec:Rect = new Rect();
rec.fill = solidColor;
rec.percentWidth = 100;
rec.percentHeight = 100;
lble.width = 127;
lble.height = 34;
lble.depth =0;
lble.addElement(rec);
lble.addEventListener(MouseEvent.CLICK, lable);
lble.addElement(myButton);
myGroup.addElement(lble);
}
private function btn(e:Event):void {
e.stopPropagation();
jj.text = 'Text For Button';
}
private function lable(e:Event):void {
kk.text = "Text For Label";
}
]]>
</fx:Script>
<s:HGroup id="myGroup">
<s:Button width="126" height="34" click="addButton();" label="Click" skinClass="spark.skins.spark.ButtonSkin"/>
</s:HGroup>
<s:Label id="jj" x="14" y="150" width="1200" height="50" backgroundColor="gray" text="Button"/>
<s:Label id="kk" x="14" y="69" width="1200" height="50" backgroundColor="gray" text="Label"/>
答案 2 :(得分:0)
据我了解 - 当您向Group添加元素时,您需要在Button上添加Button。根据我的经验,我使用下一个 - 为子元素创建主容器,然后将他添加到HGroup。请参阅下面的addButton()方法的实现。
public function addButton():void
{
//First Child Object
var myButton:Button = new Button();
myButton.id = "dd";
myButton.label = "X";
myButton.width = 20;
myButton.height = 20;
myButton.depth = 1;
myButton.x = 105;
myButton.setStyle("color",'red');
myButton.addEventListener(MouseEvent.CLICK, btn);
//Second Object
var lble:Label = new Label();
lble.width = 127;
lble.height = 34;
lble.depth = 0;
lble.setStyle("backgroundColor",'red');
lble.addEventListener(MouseEvent.CLICK, lable);
//Parent Object
var grp:Group = new Group();
grp.addElement(lble);
grp.addElement(myButton);
myGroup.addElement(grp);
}