AS3 OptionsList ---- 选项框列表类

Option类:

package
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;

	/**
	 * 列表框子控件--列表选项框(单个选项)
	 * @author Jave.Lin
	 */	
	public class Option extends Control
	{
		private var _isDrawBorder:Boolean=false;//是否绘制边框
		private var _mask:Shape;
		private var _isDowned:Boolean=false;
		private var _glowFilter:GlowFilter;
		//数据对象
		private var _data:Object;
		//显示数据对象文本
		private var _textbox:TextBox;
		
		public function get isDrawBorder():Boolean
		{
			return _isDrawBorder;
		}
		
		public function set isDrawBorder(value:Boolean):void
		{
			if(_isDrawBorder!=value)
			{
				_isDrawBorder=value;
				
				refreshBackground();
			}
		}
		
		public function get textColor():uint
		{
			return _textbox.textColor;
		}
		
		public function set textColor(value:uint):void
		{
			_textbox.textColor=value;
		}
		
		public override function get width():Number
		{
			return super.width;
		}
		
		public override function set width(value:Number):void
		{
			if(width!=value)
			{
				_w=value;
				_textbox.width=_w;
				
				refreshBackground();
			}
		}
		
		public override function get height():Number
		{
			return super.height;
		}
		
		public override function set height(value:Number):void
		{
			if(height!=value)
			{
				_h=value;
				_textbox.height=_h;
				
				refreshBackground();
			}
		}
		
		public function get data():Object
		{
			return _data;
		}
		
		public function set data($data:Object):void
		{
			if(_data!=$data)
			{
				_data=$data;
				if(_data)
				{
					_textbox.text=_data.toString();
				}
				else
				{
					_textbox.text='';
				}
				refreshBackground();
			}
		}
		
		public function Option($data:Object)
		{
			super();
			
			data=$data;
		}
		
		public function setWithAndHeight($width:Number,$height:Number):void
		{
			_textbox.width=_w=$width;
			_textbox.height=_w=$height;
			
			refreshBackground();
		}
		
		protected override function initialize():void
		{
			this.mouseChildren=false;
			
			_textbox=new TextBox();
			_textbox.isDrawBorder=false;
			_textbox.isReadOnly=true;
			_textbox.isSelectable=false;
			_textbox.fontSize=10;
			addChild(_textbox);
			
			_mask=new Shape();
			addChild(_mask);
			
			this.mask=_mask;
			
			textColor=0;
			_glowFilter=new GlowFilter(0x00ff00,1,3,3,3);
			
			if(stage)
			{
				onAddedToStageHandler();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
			}
		}
		
		private function onAddedToStageHandler(e:Event=null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
			
			addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageHandler);
			
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
		}
		
		private function onRemovedFromStageHandler(e:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageHandler);
			removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
		}
		
		private function onMouseOutHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			
			filters=null;
		}
		
		private function onMouseOverHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			
			filters=[_glowFilter];
		}
		
		protected override function refreshBackground():void
		{
			this.graphics.clear();
			
			this.graphics.beginFill(0x00ff00,.2);
			this.graphics.drawRect(0,0,width,height);	
			this.graphics.endFill();
			
			if(_isDrawBorder)
			{
				this.graphics.lineStyle(1);
				this.graphics.moveTo(0,0);
				this.graphics.lineTo(width-1,0);
				this.graphics.lineTo(width-1,height-1);
				this.graphics.lineTo(0,height-1);
				this.graphics.lineTo(0,0);
			}
			
			_mask.graphics.clear();
			
			_mask.graphics.beginFill(0x00ff00,.2);
			_mask.graphics.drawRect(0,0,width,height);	
			_mask.graphics.endFill();
		}
		
		public override function toString():String
		{
			return "Option ["+_data+"]";
		}
	}
}

OptionsList类:

package
{
	import controlsEvents.OptionsListEvent;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	/**
	 * 多选项选择框列表类
	 * @author Jave.Lin
	 */
	public class OptionsList extends Control
	{
		private var _itemsContainer:Panel;//子项容器
		private var _itemsControlDic:Dictionary;//子顶控制列表
		private var _isShow:Boolean=true;//是否显示
		private var _selectedItem:Option;
		private var _selectedIndex:int;
		
		private var _items:Vector.<Object>;//子项所有数据
		
		public function get items():Vector.<Object>
		{
			return _items;
		}
		//获取当前选中的子项控件,里面包括数据
		public function get selectedItem():Option
		{
			return _selectedItem;
		}
		//获取当前选中子项的索引
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
		//设置当前选中子项的索引
		public function set selectedIndex(value:int):void
		{
			if(_selectedIndex!=value)
			{
				if(value>_items.length-1)
				{
					throw new Error("set OptionsList.selectedIndex error,value:"+value);
				}
				if(value<0)
				{
					if(value==-1)
					{
						_selectedIndex=value;
						_selectedItem=null;
					}
					else
					{
						throw new Error("set OptionsList.selectedIndex error,value:"+value);
					}
				}
				else
				{
					_selectedIndex=value;
					_selectedItem=findOptionByData(_items[_selectedIndex]);
				}
				
				dispatchEvent(new OptionsListEvent(OptionsListEvent.SELECTED_CHANGED));
			}
		}
		
		public override function get width():Number
		{
			return super.width;
		}
		
		public override function set width(value:Number):void
		{
			if(width!=value)
			{
				_w=value;
				
				_itemsContainer.width=_w;
				for each (var o:Option in _itemsControlDic) 
				{
					o.width=_w;
				}
			}
		}
		
		public override function get height():Number
		{
			return super.height;
		}
		
		public override function set height(value:Number):void
		{
			if(height!=value)
			{
				_h=value;
				
				_itemsContainer.height=_h;
			}
		}
		//是否显示
		public function get isShow():Boolean
		{
			return _isShow;
		}
		
		public function set isShow(value:Boolean):void
		{
			if(_isShow!=value)
			{
				_isShow=value;
			}
		}
		
		public function OptionsList()
		{
			super();
		}
		
		public function addItem(obj:Object):void
		{
			if(obj==null)
			{
				throw new Error("OptionsList.addItem obj is null");
			}
			_items.push(obj);
			
			var o:Option=new Option(obj);
			o.addEventListener(MouseEvent.CLICK,onOptionClickHandler);
			_itemsContainer.addChild(o);
			_itemsControlDic[obj]=o;
			o.y=(_items.length-1)*o.height;
			o.width=_w;
			
			_itemsContainer.refreshScroller();
		}
		
		private function onOptionClickHandler(e:MouseEvent):void
		{
			var o:Option=e.target as Option;
			if(o==null)return;
			_selectedItem=o;
			selectedIndex=_items.indexOf(o.data);
		}
		
		public function addItemRange(arr:Array):void
		{
			for each (var obj:Object in arr) 
			{
				if(obj==null)
				{
					throw new Error("OptionsList.addItem obj is null");
				}
				_items.push(obj);
				
				var o:Option=new Option(obj);
				o.addEventListener(MouseEvent.CLICK,onOptionClickHandler);
				_itemsContainer.addChild(o);
				_itemsControlDic[obj]=o;
				o.y=(_items.length-1)*o.height;
				o.width=_w;
			}
			
			_itemsContainer.refreshScroller();
		}
		
		public function removeItem(obj:Object):void
		{
			if(obj==null)
			{
				throw new Error("OptionsList.removeItem obj is null");
			}
			removeOptionByData(obj);
		}
		//根据数据:删除数据及控件
		public function removeOptionByData($data:Object):Option
		{
			if($data==null)
			{
				throw new Error("OptionsList.removeComboBoxByData $data is null");
			}
			//删除数据
			var index:int=_items.indexOf($data);
			if(index!=-1)
			{
				_items.splice(index,1);
			}
			//删除控件
			var option:Option=findOptionByData($data);
			if(option)
			{
				delete _itemsControlDic[$data];//从字典中删除:控件
				_itemsContainer.removeChild(option);//从控件容器中删除:控件
			}
			onSortControl(index);
			return option;
		}
		//根据数据找
		public function findOptionByData($data:Object):Option
		{
			return _itemsControlDic[$data];
		}
		//排列控件
		private function onSortControl(startIndex:int=-1):void
		{
			var i:int=0;
			if(startIndex!=-1)i=startIndex;
			for (; i < _items.length; i++) 
			{
				var cbo:Option=findOptionByData(_items[i]);
				cbo.y=i*cbo.height;
			}
		}
		
		protected override function initialize():void
		{
			_items=new Vector.<Object>();
			_itemsControlDic=new Dictionary();
			
			_itemsContainer=new Panel();
			addChild(_itemsContainer);
			
			_itemsContainer.isUseVs=true;
		}
		
		protected override function refreshBackground():void
		{
			// nothing to do
		}
	}
}

测试类:

package test
{
	import controlsEvents.OptionsListEvent;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.utils.setInterval;
	
	public class OptionsListTest extends Sprite
	{
		private var author:Label;
		private var comboBoxOptions:OptionsList;
		
		public function OptionsListTest()
		{
			super();
			
			stage.color=0xccccbb;
			stage.frameRate=60;
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			
			author=new Label();
			addChild(author);
			author.textColor=0x00ff00;
			author.fontSize=24;
			author.x=100;
			author.y=50;
			author.text="作者:Jave.Lin";
			
			comboBoxOptions=new OptionsList();
			addChild(comboBoxOptions);
			
			comboBoxOptions.width=200;
			comboBoxOptions.height=150;
			
			comboBoxOptions.x=100;
			comboBoxOptions.y=100;
			
			var arr:Array=[];
			
			for (var i:int = 0; i < 20; i++) 
			{
				arr[i]="test"+i;
			}
			comboBoxOptions.addItemRange(arr);
			
			comboBoxOptions.addEventListener(OptionsListEvent.SELECTED_CHANGED,onSelectedChangedHandler);
//			setInterval(function():void{comboBoxOptions.removeItem("test5");},1000);
//			setInterval(function():void{comboBoxOptions.removeItem("test1");},2000);
		}
		
		private function onSelectedChangedHandler(e:OptionsListEvent):void
		{
			var cbos:OptionsList=e.target as OptionsList;
			if(cbos==null)return;
			trace("cbos",cbos);
			trace("cbos.selectedIndex",cbos.selectedIndex);
			trace("cbos.selectedItem",cbos.selectedItem);
		}
	}
}

运行图片效果:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 第 1 章 : 简介 目标读者 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 系统要求 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 关于本文档 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 印刷惯例 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 本手册中使用的术语 . . . . . . . . . . . . . . . . . . . . . . . . 2 其他资源 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 第 2 章 : 关于 ActionScript 3.0 组件 使用组件的优点 . . . . . . . . . . . . . . . . . . . . . . . . . . 3 组件型 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 在文档中添加和删除组件 . . . . . . . . . . . . . . . . . . . . . . 5 查找组件的版本 . . . . . . . . . . . . . . . . . . . . . . . . . . 7 ActionScript 3.0 事件处理模型 . . . . . . . . . . . . . . . . . . . 7 一个简单的应用程序 . . . . . . . . . . . . . . . . . . . . . . . . 8 第 3 章 : 使用组件 组件体系结构 . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 使用组件文件 . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 调试组件应用程序 . . . . . . . . . . . . . . . . . . . . . . . . . 17 设置参数和属性 . . . . . . . . . . . . . . . . . . . . . . . . . . 17 库 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 调整组件大小 . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 实时预览 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 处理事件 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 使用显示列表 . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 使用 FocusManager . . . . . . . . . . . . . . . . . . . . . . . . 23 使用基于 List 的组件 . . . . . . . . . . . . . . . . . . . . . . . 24 使用 DataProvider . . . . . . . . . . . . . . . . . . . . . . . . 24 使用 CellRenderer . . . . . . . . . . . . . . . . . . . . . . . . 31 使组件具有辅助功能 . . . . . . . . . . . . . . . . . . . . . . . . 37 第 4 章 : 使用 UI 组件 使用 Button 组件 . . . . . . . . . . . . . . . . . . . . . . . . . 39 使用 CheckBox 组件 . . . . . . . . . . . . . . . . . . . . . . . . 41 使用 ColorPicker 组件 . . . . . . . . . . . . . . . . . . . . . . .43 使用 ComboBox 组件 . . . . . . . . . . . . . . . . . . . . . . . . 45 使用 DataGrid 组件 . . . . . . . . . . . . . . . . . . . . . . . . 48 使用 Label 组件 . . . . . . . . . . . . . . . . . . . . . . . . . 53 使用 List 组件 . . . . . . . . . . . . . . . . . . . . . . . . . . 55 使用 NumericStepper 组件 . . . . . . . . . . . . . . . . . . . . . 59 使用 ProgressBar 组件 . . . . . . . . . . . . . . . . . . . . . . 61 使用 RadioButton 组件 . . . . . . . . . . . . . . . . . . . . . . 66 使用 ScrollPane 组件 . . . . . . . . . . . . . . . . . . . . . . . 69 使用 Slider 组件 . . . . . . . . . . . . . . . . . . . . . . . . . 71 使用 TextArea 组件 . . . . . . . . . . . . . . . . . . . . . . . . 73 使用 TextInput 组件 . . . . . . . . . . . . . . . . . . . . . . . 76 使用 TileList 组件 . . . . . . . . . . . . . . . . . . . . . . . . 78 使用 UILoader 组件 . . . . . . . . . . . . . . . . . . . . . . . . 81 使用 UIScrollBar 组件 . . . . . . . . . . . . . . . . . . . . . . .82 第 5 章 : 自定义 UI 组件 关于 UI 组件自定义 . . . . . . . . . . . . . . . . . . . . . . . . 85 设置样式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 关于外观 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 自定义 Button 组件 . . . . . . . . . . . . . . . . . . . . . . . . 89 自定义 CheckBox 组件 . . . . . . . . . . . . . . . . . . . . . . . 91 自定义 ColorPicker 组件 . . . . . . . . . . . . . . . . . . . . . 93 自定义 ComboBox 组件 . . . . . . . . . . . . . . . . . . . . . . . 94 自定义 DataGrid 组件 . . . . . . . . . . . . . . . . . . . . . . . 96 自定义 Label 组件 . . . . . . . . . . . . . . . . . . . . . . . . 100 自定义 List 组件 . . . . . . . . . . . . . . . . . . . . . . . . 101 自定义 NumericStepper 组件 . . . . . . . . . . . . . . . . . . . 104 自定义 ProgressBar 组件 . . . . . . . . . . . . . . . . . . . . . 105 自定义 RadioButton 组件 . . . . . . . . . . . . . . . . . . . . . 107 自定义 ScrollPane 组件 . . . . . . . . . . . . . . . . . . . . . 108 自定义 Slider 组件 . . . . . . . . . . . . . . . . . . . . . . . 109 自定义 TextArea 组件 . . . . . . . . . . . . . . . . . . . . . . 110 自定义 TextInput 组件 . . . . . . . . . . . . . . . . . . . . . . 112 自定义 TileList 组件 . . . . . . . . . . . . . . . . . . . . . . 113 自定义 UILoader 组件 . . . . . . . . . . . . . . . . . . . . . . 115 自定义 UIScrollBar 组件 . . . . . . . . . . . . . . . . . . . . . 115 第 6 章 : 使用 FLVPlayback 组件 使用 FLVPlayback 组件 . . . . . . . . . . . . . . . . . . . . . . 117 自定义 FLVPlayback 组件 . . . . . . . . . . . . . . . . . . . . . 132 使用 SMIL 文件 . . . . . . . . . . . . . . . . . . . . . . . . . 141 第 7 章 : 使用 FLVPlayback 字幕组件 使用 FLVPlaybackCaptioning 组件 . . . . . . . . . . . . . . . . . 148 使用 Timed Text 字幕 . . . . . . . . . . . . . . . . . . . . . . 150 将提示点用于字幕 . . . . . . . . . . . . . . . . . . . . . . . . 155

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值