100分求动态添加Button的例子

艾思软件-app开发公司 2009-09-23 11:33:12
要求用for语句动态生成Button,并且对每个Button设置不同的ID,绑定事件,并在绑字事件的同时传递不同的参数

写个例子

for(var i:int=0;i<3;i++){
//生成Button
//确定Button的ID为"btn"+i
//绑定"btn"+i的click事件为 btnClick(i)
}

function btnClick(i:int):void{
Alert.show(i.toString());
}
...全文
47 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
感激之情无法用语言形容
jerryyu810 2009-09-23
  • 打赏
  • 举报
回复

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()"
width="800" height="600">
<mx:Script>
<![CDATA[
import mx.controls.TextArea;
import mx.controls.Button;
import mx.controls.Alert;


internal function initApp():void{
for(var i:int=0;i<5;i++)
{
var btn:Button=new Button();
btn.id="btn"+i;
btn.label = "" + i;
btn.width = 50;
btn.x = i*52;//两个button之间间隔2个像素

btn.addEventListener(MouseEvent.CLICK,btnClickHandler);
addChild(btn);
}
}
private function btnClickHandler(event:MouseEvent):void
{

}

]]>
</mx:Script>
</mx:Application>
yangyawen 2009-09-23
  • 打赏
  • 举报
回复

private function test():void{
for(var i:int=0;i<3;i++){
var button:Button=new Button();
button.id="a"+i;
button.addEventListener(MouseEvent.CLICK, handleEvent);
p.addChild(button);//p为用来装button的容器
}
}
private function handleEvent(event:MouseEvent):void{
Alert.show(event.currentTarget.id+"===="+event.target.id);
}
手抓宝 2009-09-23
  • 打赏
  • 举报
回复
所有的Button注册一个侦听就好,通过event进行区别
手抓宝 2009-09-23
  • 打赏
  • 举报
回复

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.messaging.AbstractConsumer;
import mx.controls.Button;
import mx.controls.Alert;

private function init():void
{
for(var i:int=0;i<20;i++)
{
var btn:Button=new Button();
btn.id="btn"+i;
btn.addEventListener(MouseEvent.CLICK,btnClickHandler);
}
}
private function btnClickHandler(event:MouseEvent):void
{
Alert.show(Button(event).id);
}
]]>
</mx:Script>
</mx:Application>

4,328

社区成员

发帖
与我相关
我的任务
社区描述
多媒体/设计 Flex
社区管理员
  • Flex
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧