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

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>

你可能感兴趣的:(Flex)