效果图如下:
/**
2008-03-22 风帆扬清 QQ:332612636 Email:[email protected]
**/
package documentClasses
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.ApplicationDomain;
import flash.utils.Dictionary;
public class SearchField extends MovieClip
{
private var domain : ApplicationDomain;
public var searchTxt_mc : MovieClip; //搜索条件输入文本
public var allProjects_mc : MovieClip; //搜索条件下拉列表
public var submit_mc : MovieClip; //确定按钮
public var itemCollection : Sprite; //显示搜索条件下拉列表值的影片
public var searchList : Dictionary; //ComboBox的内容
public var listIndex : int; //ComboBox index
public var listOpened : Boolean; //搜索条件下拉列表是否打开
public var listBaseHeight : int; //开始位置
public var listUnitHeight : uint; //单位高度
public var listHeight : uint; //下拉框高度
public function SearchField()
{
super();
init();
}
private function init() : void
{
domain = this.loaderInfo.applicationDomain;
listBaseHeight = 30;
listUnitHeight = 30;
listOpened = false;
initData();
searchTxt_mc._input.addEventListener(MouseEvent.CLICK, inputActivateHandler); //搜索条件输入文本中输入时,文本显示为活动状态
searchTxt_mc._input.addEventListener(Event.CHANGE, inputChangeHandler); //搜索条件输入文本中输入改变时,所触发的事件
allProjects_mc.addEventListener(MouseEvent.CLICK, listOpenedHandler,true); //点击搜索条件下拉框时,所触发的事件
submit_mc.addEventListener(MouseEvent.ROLL_OVER, submitRollOverHandler); //提交
}
//----------------------
//搜索条件输入文本的显示逻辑
//-------------------------
protected function inputActivateHandler(evt : MouseEvent) : void
{
if(searchTxt_mc.currentLabel == "notyping")
{
searchTxt_mc.gotoAndPlay("in");
}
}
protected function inputChangeHandler(evt : Event) : void
{
if(searchTxt_mc._input.text =="")
{
searchTxt_mc.gotoAndPlay("in");
}
else
{
searchTxt_mc.gotoAndPlay("out");
}
}
//----------------------
//搜索条件下拉框的显示逻辑
//-------------------------
protected function CBRollOverHandler(evt : MouseEvent) : void
{
evt.target.parent.gotoAndPlay("in");
evt.target.textColor = 0xff00ff;
//滑鼠滑入时,注册滑出和点击事件
evt.target.addEventListener(MouseEvent.ROLL_OUT, CBRollOutHandler);
evt.target.addEventListener(MouseEvent.CLICK, CBClickHandler,true);
}
protected function CBRollOutHandler(evt : MouseEvent) : void{
evt.target.parent.gotoAndPlay("out");
evt.target.textColor = 0x000000;
//滑鼠滑出时,删除滑出和点击事件
evt.target.removeEventListener(MouseEvent.ROLL_OUT, CBRollOutHandler);
evt.target.removeEventListener(MouseEvent.CLICK, CBClickHandler);
}
protected function CBClickHandler(evt : MouseEvent) : void{
if(evt.target.text is String)
allProjects_mc.searchCbValue_txt.text = evt.target.text;
listOpened = false; //All Projects 列表关闭
allProjects_mc.background_mc.items_mc.graphics.clear(); //清空列表背景
allProjects_mc.background_mc.gotoAndPlay("empty"); //删除列表文本
}
protected function listOpenedHandler(evt : MouseEvent):void
{
if(! listOpened){
initComboBox();
listOpened = true;
}
else
{
CBClickHandler(evt);
listOpened = false;
}
}
//---------------------------------
//提交按钮显示逻辑
//---------------------------------
protected function submitRollOverHandler(evt : MouseEvent):void
{
submit_mc.gotoAndPlay("in");
//滑鼠滑入时,注册滑出和点击事件
submit_mc.addEventListener(MouseEvent.ROLL_OUT, submitRollOutHandler);
submit_mc.addEventListener(MouseEvent.CLICK, submitClickHandler);
}
protected function submitRollOutHandler(evt : MouseEvent):void
{
submit_mc.gotoAndStop("default");
//滑鼠滑出时,删除滑出和点击事件
submit_mc.removeEventListener(MouseEvent.ROLL_OUT , submitRollOutHandler);
submit_mc.removeEventListener(MouseEvent.CLICK , submitClickHandler);
}
protected function submitClickHandler(evt : MouseEvent):void
{
//提交数据,返回结果
trace("txt is :" + searchTxt_mc._input.text);
trace("select is :" + allProjects_mc.searchCbValue_txt.text);
}
//-------------------------
//绘制ComboBox
//------------------
internal function initComboBox() : void
{
//取得影片容器
var mc:Sprite = allProjects_mc.background_mc.items_mc;
try {mc.removeChild ( itemCollection );}catch(e:Error){}
itemCollection = new Sprite();
mc.addChild(itemCollection);
//绘制背景
mc.graphics.clear();
mc.graphics.beginFill(0x000000);
mc.graphics.drawRect( 0 , listBaseHeight , 170 , listHeight);
mc.graphics.beginFill(0xFFFFFF);
mc.graphics.drawRect( 1 , listBaseHeight , 168 , listHeight - 1);
//生成列表文本对象
var itemShape : Class = domain.getDefinition("itemEffect") as Class;
var items : Array = this.items;
var itemsLength : int = items.length;
for(var i=0;i<itemsLength;i++)
{
var shape:MovieClip = new itemShape();
shape.x = 2;
shape.y = i * listUnitHeight + listBaseHeight;
shape.label.text = ""+searchList[i].name+" value : "+ searchList[i].value;
shape.label.addEventListener(MouseEvent.ROLL_OVER, CBRollOverHandler);
//shape.addEventListener(MouseEvent.ROLL_OUT, CBRollOutHandler); //放在滑鼠滑入时
//shape.addEventListener(MouseEvent.CLICK, CBClickHandler); //放在滑鼠滑入时
itemCollection.addChild(shape);
}
}
//---------------------------------------------------------
//业务逻辑
//----------------------------------------------------
public function initData() : void
{
searchList = new Dictionary ( true );
addItem({name:"name 1" , value:"value 1"});
addItem({name:"name 2" , value:"value 2"});
addItem({name:"name 3" , value:"value 3"});
addItem({name:"name 4" , value:"value 4"});
addItem({name:"name 5" , value:"value 5"});
}
public function addItem ( data : Object ) : void {
if ( data==null || data.name == null ||
data.value == null || !data.name is String ||
!data.value is String ) {
throw new Error ( "格式错误{name:\"String\",value:\"String\"}" );
return;
}
searchList[listIndex] = data;
listIndex++;
listHeight = listIndex * listUnitHeight;
}
public function get items():Array
{
var result:Array = new Array();
for each(var i:* in searchList)
{
result.push(i);
}
return result;
}
}
}