flex中为控件添加监听器并计算

时间:2022-07-26 05:32:48

1、添加监听器:

public function moduleCreationComplete():void
{
this.D601_29a.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_28);
this.D601_30a.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_28);
this.D601_31a.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_28);
this.D601_29b.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_28);
this.D601_30b.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_28);
this.D601_31b.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_28); this.D601_36.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_35);
this.D601_37.addEventListener(FlexEvent.SELECTION_CHANGE,setD601_35);
this.D601_65.addEventListener(FlexEvent.CHANGING,setD601_07);
this.D601_65.addEventListener(FlexEvent.ENTER,setD601_07);
}

2、写计算函数:

public function setD601_28(event:Event):void
{//计算小时分钟
var firsta:Number = Number(this.D601_29a.text);
var firstb:Number = Number(this.D601_29b.text);
var seconda:Number = Number(this.D601_30a.text);
var secondb:Number = Number(this.D601_30b.text);
var thirda:Number = Number(this.D601_31a.text);
var thirdb:Number = Number(this.D601_31b.text); this.D601_28a.text = (Math.floor(((firsta + seconda + thirda)*60 +firstb + secondb + thirdb) / 60)).toString();
this.D601_28b.text = (((firsta + seconda + thirda)*60 +firstb + secondb + thirdb) % 60).toString();
}
public function setD601_35(event:Event):void
{
var chanshi:Number = Number(this.D601_36.text);
var chanhou:Number = Number(this.D601_37.text); this.D601_35.text = (chanshi + chanhou).toString();
}
public function setD601_07(event:Event):void
{//根据身份证号计算年龄
var cardId:String = this.D601_65.text;
if(cardId.length >= 16)
{
var year:Number = Number(this.D601_65.text.substr(6,4));
var month:Number = Number(this.D601_65.text.substr(10,2));
var day:Number = Number(this.D601_65.text.substr(12,2));
var date:Date = new Date();
var nowMonth = date.month + 1;
var age = date.fullYear - year;
if(month > nowMonth || month == nowMonth && day >= date.date)
{
age++;
}
this.D601_07.text=age;
}
}