flex 动态给控件赋值,通过反射遍历MXML中的组件

本文介绍了一种使用Flex的反射机制来动态地给多个无规律ID的TextInput控件赋值的方法。这种方法适用于当需要批量设置属性而无法直接通过循环实现的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

flex 动态给控件赋值,通过反射遍历MXML中的组件。当有100个mx:TextInput 需要给他们text属性赋值的时候,如果id存在规律可以用for,但如果id完全没规律的话那麽就需要用下面方法了,通过反射

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Canvas id="s" width="263" height="193" backgroundImage="../img/infobg.png">
<mx:Canvas id="k" width="243" height="141.1" x="10" y="41.9">
<mx:Script>
<![CDATA[
import mx.controls.TextInput;
import mx.controls.Button;
public function dkdk(){
var instanceInfo:XML=describeType(this);//通过反射机制取出当前MXML中的信息
var properties:XMLList =instanceInfo..accessor.(@type=="mx.controls::TextInput")
trace(instanceInfo..accessor.(@type=="mx.controls::TextInput"));
for each(var propertyInfo:XML in properties){
var propertyName:String =propertyInfo.@name;//此处取出的为textinput的id
TextInput(this[propertyName]).text="1111";
}
}
]]>
</mx:Script>
<mx:TextInput id="a" x="0" y="1" width="90" height="17.6"/>
<mx:TextInput id="b" x="89.05" y="1" width="117.899994" height="17.6"/>
<mx:TextInput x="0" y="20" width="90" height="17.6"/>
<mx:TextInput x="89.05" y="20" width="117.899994" height="17.6"/>
<mx:TextInput x="0" y="39" width="90" height="17.6"/>
<mx:TextInput x="89.05" y="38" width="117.899994" height="17.6"/>
<mx:TextInput x="0" y="58" width="90" height="17.6"/>
<mx:TextInput x="89.05" y="58" width="117.899994" height="17.6"/>
<mx:TextInput x="0" y="77" width="90" height="17.6"/>
<mx:TextInput x="89.05" y="77" width="117.899994" height="17.6"/>
<mx:TextInput x="0" y="96" width="90" height="17.6"/>
<mx:TextInput x="89.05" y="96" width="117.899994" height="17.6"/>
<mx:TextInput id="p" x="0" y="115" width="90" height="17.6"/>
<mx:TextInput x="89.05" y="115" width="117.899994" height="17.6"/>
</mx:Canvas>
</mx:Canvas>
<mx:Button x="332" y="107" label="Button" click="dkdk()"/>
</mx:Application>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值