flex 点击按钮图片切换

2019-04-15 16:49发布

  1. "1.0" encoding="utf-8"?>
  2. "http://www.adobe.com/2006/mxml" layout="absolute">
  3.     
  4.         import mx.controls.*;
  5.             private var num:int;
  6.             private function pic(direction:String):void{
  7.                 if(direction == 'next'){
  8.                     this.num++;
  9.                     if (this.num >= xmlPic.pic.length()){this.num = xmlPic.length()-1;}
  10.                 }
  11.                 if(direction == 'prev'){
  12.                     this.num--;
  13.                     if(this.num < 0){this.num = 0;}
  14.                 }       
  15.                 myimg.load(xmlPic.pic[num].@path);//这一句也可这样写 myimg.load((xmlPic.children())[num].@path);
  16.             }
  17.     ]]>
  18. "xmlPic" source="data/pic.xml" />
  19. "0" width="400" height="300">
  20.         "0" width="100%" height="100%">
  21.         "100%" height="100%" horizontalAlign="center" verticalAlign="middle">            
  22.             "myimg" width="158" height="120" source="{xmlPic.pic[0].@path}"/>  
  23.             
  24.                 "prev" label="上一张" click="pic('prev')"/>      
  25.                 "next" label="下一张" click="pic('next')"/>
  26.                       
  27.                   
  28.                 
  29.          
  xml文件pic.xml
  1. xml version="1.0" encoding="utf-8"?> 
  2. <pics>
  3.     <pic name="图1" path="assets/small/1.jpg"/>
  4.     <pic name="图2" path="assets/small/2.jpg"/>
  5.     <pic name="图3" path="assets/small/3.jpg"/>
  6.     <pic name="图4" path="assets/small/4.jpg"/>
  7.     <pic name="图5" path="assets/small/5.jpg"/>
  8.     <pic name="图6" path="assets/small/6.jpg"/>
  9.     <pic name="图7" path="assets/small/7.jpg"/>
  10.     <pic name="图8" path="assets/small/8.jpg"/>
  11. pics>