ComboBoxDropDownButton类(下拉列表框的按钮类):
package
{
/**
* 下拉列表框的按钮类
* @author Jave.Lin
*/
public class ComboBoxDropDownButton extends Button
{
private var _rateX:Number=1;
private var _rateY:Number=1;
public override function set width(value:Number):void
{
if(super.width!=value)
{
super.width=value;
_rateX=value/10;
refreshBackground();
}
}
public override function set height(value:Number):void
{
if(super.height!=value)
{
super.height=value;
_rateY=value/10;
refreshBackground();
}
}
public function ComboBoxDropDownButton()
{
super();
width=10;
height=10;
}
protected override function refreshBackground():void
{
//super.refreshBackground();
//背景也居中
this.graphics.clear();
this.graphics.beginFill(0x55aa00, 0.1);
this.graphics.drawRect(0,0,10*_rateX,10*_rateY);
this.graphics.endFill();
//边框
this.graphics.moveTo(0,0);
this.graphics.lineStyle(1,0,.5);
this.graphics.lineTo(10*_rateX,0);
this.graphics.lineTo(10*_rateX,10*_rateY);
this.graphics.lineTo(0,10*_rateY);
this.graphics.lineTo(0,0);
//三角形
this.graphics.moveTo(5*_rateX,8*_rateY);
this.graphics.lineStyle(1,0,.5);
this.graphics.lineTo(8*_rateX,2*_rateY);
this.graphics.lineTo(2*_rateX,2*_rateY);
this.graphics.lineTo(5*_rateX,8*_rateY);
}
}
}
ComboBox下拉列表框类:
package
{
import controlsEvents.ComboBoxEvent;
import controlsEvents.OptionsListEvent;
import flash.display.Bitmap;
import flash.display.Shape;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
/**
* 下拉列表框类
* @author Jave.Lin
*/
public class ComboBox extends Control
{
private var _background:Shape;//背景
private var _glowFilter:GlowFilter;
private var _showSelectedItem:Option;//显示在当前选中框中的子项
private var _optionsList:OptionsList;
private var _dropDownBtn:ComboBoxDropDownButton;//下拉按钮
public override function get width():Number
{
return super.width;
}
public override function set width(value:Number):void
{
if(width!=value)
{
_w=value;
if(_w<(_dropDownBtn.width*2))_w=_dropDownBtn.width*2;
onLayout();
refreshBackground();
}
}
public override function get height():Number
{
return super.height;
}
public override function set height(value:Number):void
{
if(height!=value)
{
_h=value;
if(_h<_dropDownBtn.height)_h=_dropDownBtn.height;
onLayout();
refreshBackground();
}
}
//获取当前选中的子项控件,里面包括数据
public function get selectedItem():Option
{
return _optionsList.selectedItem;
}
//获取当前选中子项的索引
public function get selectedIndex():int
{
return _optionsList.selectedIndex;
}
//设置当前选中子项的索引
public function set selectedIndex(value:int):void
{
_optionsList.selectedIndex=value;
}
public function get items():Vector.<Object>
{
return _optionsList.items;
}
public function ComboBox()
{
super();
}
public function addItem(obj:Object):void
{
_optionsList.addItem(obj);
}
public function addItemRange(arr:Array):void
{
_optionsList.addItemRange(arr);
}
public function removeItem(obj:Object):void
{
_optionsList.removeItem(obj);
}
private function onLayout():void
{
_showSelectedItem.width=_w-_dropDownBtn.width;
_showSelectedItem.height=_h;
_dropDownBtn.height=_h;
_dropDownBtn.width=_dropDownBtn.height=_showSelectedItem.height;
_dropDownBtn.x=_showSelectedItem.width;
_optionsList.y=_dropDownBtn.height;
_optionsList.width=_w;
}
protected override function initialize():void
{
_background=new Shape();
addChild(_background);
//当前选中显示项
_showSelectedItem=new Option('');
addChild(_showSelectedItem);
_showSelectedItem.isDrawBorder=true;
//下拉按钮
_dropDownBtn=new ComboBoxDropDownButton();
addChild(_dropDownBtn);
//下拉列表
_optionsList=new OptionsList();
addChild(_optionsList);
_optionsList.visible=false;
_glowFilter=new GlowFilter(0x00ff00,1,3,3,3);
_optionsList.addEventListener(OptionsListEvent.SELECTED_CHANGED,onOptionsSelectedChangedHandler);
_w=100;
_h=15;
onLayout();
if(stage)
{
onAddedToStageHandler();
}
else
{
addEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
}
}
private function onOptionsSelectedChangedHandler(e:OptionsListEvent):void
{
_optionsList.visible=false;
_showSelectedItem.data=_optionsList.selectedItem.data;
dispatchEvent(new ComboBoxEvent(ComboBoxEvent.SELECTED_CHANGED));
}
private function onClickHandler(e:MouseEvent):void
{
_optionsList.visible=!_optionsList.visible;
}
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);
_showSelectedItem.addEventListener(MouseEvent.CLICK,onClickHandler);
_dropDownBtn.addEventListener(MouseEvent.CLICK,onClickHandler);
}
private function onRemovedFromStageHandler(e:Event):void
{
removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageHandler);
removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
_dropDownBtn.addEventListener(MouseEvent.CLICK,onClickHandler);
_showSelectedItem.addEventListener(MouseEvent.CLICK,onClickHandler);
}
private function onMouseOutHandler(e:MouseEvent):void
{
removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
//如果鼠标移开控件时,是可见的,那么注册,当鼠标移动舞台外,非控件内单击时隐藏
if(_optionsList.visible)stage.addEventListener(MouseEvent.CLICK,onStageClick);
_background.filters=null;
}
private function onStageClick(e:Event):void
{
_optionsList.visible=false;
}
private function onMouseOverHandler(e:MouseEvent):void
{
removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
stage.removeEventListener(MouseEvent.CLICK,onStageClick);
_background.filters=[_glowFilter];
}
protected override function refreshBackground():void
{
_background.graphics.clear();
_background.graphics.beginFill(0x00ff00,0.1);
_background.graphics.drawRect(0,0,width,_dropDownBtn.height);
_background.graphics.endFill();
}
}
}
测试类:
package test
{
import controlsEvents.ComboBoxEvent;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
public class ComboBoxTest extends Sprite
{
private var author:Label;
private var cbx:ComboBox;
public function ComboBoxTest()
{
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";
cbx=new ComboBox();
addChild(cbx);
cbx.x=100;
cbx.y=100;
var arr:Array=[];
for (var i:int = 0; i < 100; i++)
{
arr[i]=("test"+i);
}
cbx.addItemRange(arr);
cbx.addEventListener(ComboBoxEvent.SELECTED_CHANGED,onSelectedChangedHandler);
}
private function onSelectedChangedHandler(e:ComboBoxEvent):void
{
var tcbx:ComboBox=e.target as ComboBox;
if(tcbx==null)return;
trace(tcbx);
trace("tcbx.selectedIndex",tcbx.selectedIndex);
trace("tcbx.selectedItem",tcbx.selectedItem);
}
}
}
运行图片效果: