flex自定义日期控件算法实现
前段时间在一间小公司里做过一段时间,其中有一项是实现一个自定义的日期控件,原因是flex本身并没有项目需求上需要的现成的控件提供,于是上网搜索了一下,除了js的实现一大把外,as的都没有(有的话可以留言告诉我,。。),想继承flex的日期控件改造,似乎又觉得有点过于麻烦,于是自己根据日期的算法,写了个日期控件,以下是算法的简单实现:
?
package date{import flash.events.Event;//create by minn qq:394286006 2010-12-11public class DateSelectEvent extends Event{public static var DATE_SELECT_CHANGE:String="dateSelectChange";private var _data:Object;public function DateSelectEvent(type:String,_d:Object, bubbles:Boolean=false, cancelable:Boolean=false){super(type, bubbles, cancelable);this._data=_d;}public function set data(da:Object):void{this._data=da;}public function get data():Object{return this._data;}}}
?
<?xml version="1.0" encoding="utf-8"?><mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="190" verticalGap="0" fontSize="15" height="190" cornerRadius="5" creationComplete="completeHandler(event)" backgroundColor="#F5F2F2"><fx:Declarations><!-- Place non-visual elements (e.g., services, value objects) here --></fx:Declarations><fx:Script><![CDATA[//create by minn qq:394286006 2010-12-11import mx.controls.Alert;import mx.controls.Button;import mx.events.FlexEvent;import mx.managers.PopUpManager;private var currentyear:String;private var currentmonth:String;private var currentday:String;private var selyear:String;private var selmonth:String;private var selday:String;private function TimeShow():void{var d:Date=new Date(); currentyear=d.getFullYear().toString();currentmonth=(d.getMonth()+1).toString();currentday=d.getDate().toString();if(int(currentday)<10)currentday="0"+currentday;}private var hourses:Array=[];private var mins:Array=[];private var seconds:Array=[];private function completeHandler(evt:FlexEvent):void{TimeShow();year_id.text=currentyear;month_id.text=currentmonth;go2date(int(currentyear),int(currentmonth)-1);}private function go2date(curyear:Number,curmonth:Number):void{var curD:Date=new Date(curyear,curmonth,1);var curmaxday:Number=maxDay(curD);var row:Number=curmaxday/7;var firstdaycurmonth:Number=curD.getDay();var countday:int=1;for(var i:int=0;i<row;i++){var hb:HBox=new HBox();hb.height=18;hb.setStyle("horizontalGap",8);for(var j:int=0;j<7;j++){var b:DrawDate=new DrawDate();b.width=18;b.width=18if(countday<=curmaxday){if(i==0){if(j>=firstdaycurmonth){if(countday<10)b.text="0"+countday+'';else{b.text=countday+'';}countday++;}}else {if(countday<10)b.text="0"+countday+'';elseb.text=countday+'';countday++;}}hb.addChild(b);}day_id.addChild(hb);}}private function preyear_clickHandler():void{day_id.removeAllChildren();var m:int=int(month_id.text);var y:int=int(year_id.text);year_id.text=(y-1).toString();go2date(y-1,m);}private function nextyear_clickHandler():void{day_id.removeAllChildren();var m:int=int(month_id.text);var y:int=int(year_id.text);year_id.text=(y+1).toString();go2date(y+1,m);}private function premonth_clickHandler():void{day_id.removeAllChildren();var m:int=int(month_id.text);var y:int=int(year_id.text);if(m==1){m=12;y=y-1;year_id.text=y.toString();}month_id.text=(m-1).toString();go2date(y,m-1);}private function nextmonth_clickHandler():void{day_id.removeAllChildren();var m:int=int(month_id.text);var y:int=int(year_id.text);if(m==12){m=0;year_id.text=(y+1).toString();} month_id.text=(m+1).toString();go2date(y,m);}private function maxDay(d:Date):Number{var temp:Number=0;var year:Number=d.getFullYear();var month:Number=d.getMonth()+1;if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){temp= 31;}else if(month==4||month==6||month==9||month==11){temp= 30}else if(month==2){var dd:Date=new Date(year+"/2/29");if(dd.getMonth()==1)temp= 29;else temp= 28;}return temp;}]]></fx:Script><mx:HBox horizontalGap="0" paddingTop="8" paddingLeft="5" paddingRight="5" backgroundColor="#81B6F8" color="#FEFEFE"><mx:Label useHandCursor="true" buttonMode="true" click="preyear_clickHandler()" text="《《"/><s:Label id="year_id" text="2010" paddingTop="1" /><s:Label text="年"/><mx:Label useHandCursor="true" text="》》" buttonMode="true" click="nextyear_clickHandler()"/><mx:Spacer width="16"/><mx:Label useHandCursor="true" text="《《" buttonMode="true" click="premonth_clickHandler()"/><s:Label id="month_id" text="12" paddingTop="1" width="16"/><s:Label text="月"/><mx:Label text="》》" useHandCursor="true" buttonMode="true" click="nextmonth_clickHandler()"/><mx:Spacer width="6"/></mx:HBox><mx:HBox paddingLeft="6" fontSize="18" width="100%" backgroundColor="#9ADFF9"><s:Label text="日"/><s:Label text="一"/><s:Label text="二"/><s:Label text="三"/><s:Label text="四"/><s:Label text="五"/><s:Label text="六"/></mx:HBox><mx:VBox paddingLeft="5" id="day_id"/></mx:VBox>
?
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:date="date.*"><fx:Declarations><!-- Place non-visual elements (e.g., services, value objects) here --></fx:Declarations><date:LocalDateChooser x="136" y="55" width="343" height="275"></date:LocalDateChooser></s:Application>
?