<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:base="library://apps.czw.com/flex/base"
xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%"
skinClass="com.czw.apps.ui.skin.FilletsTitleWindow"
creationComplete="init()">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Metadata>
[Event (name="saveValue", type="com.czw.apps.common.CzwEvent")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import com.czw.apps.common.CzwEvent;
import mx.controls.Alert;
import mx.controls.HRule;
import mx.utils.StringUtil;
import spark.components.TextInput;
import spark.events.TextOperationEvent;
private var _defaultRows:int=5;
private var textArr:Array=new Array;
[Bindable]
private var _fountainhead:Object;
[Bindable]
public var enabledFlag:Boolean;
public function set fountainhead(_fountainhead:Object):void
{
this._fountainhead=_fountainhead;
}
public function get fountainhead():Object
{
return this._fountainhead;
}
public function set defaultRows(_defaultRows:int):void
{
this._defaultRows=_defaultRows;
}
private function init():void
{
for (var i:int=0;i<_defaultRows;i++)
{
creatEditer();
}
}
private function creatEditer():void
{
var textInput:TextInput=new TextInput();
textInput.setStyle("borderVisible","false");
textInput.width=fieldSet.width-10;
textInput.addEventListener(KeyboardEvent.KEY_DOWN,textInputKeyDownHandle);
textInput.addEventListener(TextOperationEvent.CHANGE,validateEmail);
var hr:HRule=new HRule();
hr.width=fieldSet.width-10;
group.addElement(textInput);
group.addElement(hr);
textArr.push(textInput);
}
private function textInputKeyDownHandle(e:KeyboardEvent):void
{
var t:TextInput=e.currentTarget as TextInput;
var i:int=textArr.lastIndexOf(t,textArr.length);
if(e.charCode == 13)
{
if(i!=-1)
{
if(i==textArr.length-1)
{
creatEditer();
textArr[i+1].setFocus();
}else if(i<textArr.length-1)
{
textArr[i+1].setFocus();
}
}
}else if(e.charCode==8&&StringUtil.trim(t.text)=="")
{
if(textArr.length>_defaultRows)
{
var h:int=group.getElementIndex(t);
group.removeElementAt(h+1);
group.removeElementAt(h);
textArr[i-1].setFocus();
textArr.pop();
}else if(i>0)
{
textArr[i-1].setFocus();
}
}
}
protected function group_addedHandler(event:Event):void
{
group.validateNow();
group.verticalScrollPosition=uint.MAX_VALUE;
}
protected function button1_clickHandler(event:MouseEvent):void
{
if(!validateEmail(null))
{
Alert.show("请输入正确的邮箱地址","温馨提示");
return;
}
this.dispatchEvent(new CzwEvent("saveValue",getValue()));
this.visible=false;
}
private function getValue():String
{
var str:String="";
for (var i:int=0;i<textArr.length;i++)
{
var textValue:String=textArr[i].text;
if(StringUtil.trim(textValue)!="")
{
str+=textValue+";";
}
}
return str.substring(0,str.length-1);
}
public function setValue(str:String):void
{
cleanValue();
group.verticalScrollPosition=0;
var arr:Array=str.split(";");
var rows:int=arr.length-textArr.length;
for (var j:int=0;j<rows;j++)
{
creatEditer();
}
for (var i:int=0;i<arr.length;i++)
{
textArr[i].text=arr[i];
}
}
private function cleanValue():void
{
for (var i:int=0;i<textArr.length;i++)
{
textArr[i].text="";
textArr[i].errorString=null;
}
}
private const regex:RegExp = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
private function validateEmail(e:TextOperationEvent):Boolean {
var flag:Boolean=true;
for each(var textInput:TextInput in textArr)
{
if(StringUtil.trim(textInput.text)!="")
{
if (regex.test(textInput.text)) {
textInput.errorString = null;
} else {
textInput.errorString = "请输入正确的邮箱地址";
flag=false;
}
}else {
textInput.errorString = null;
}
}
return flag;
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
</s:layout>
<base:FieldSet legend="注:每一行为一个邮件地址"
id="fieldSet"
cornerRadius="8" gap="6" height="85%" width="95%" skinClass="com.czw.apps.ui.fieldSet.skins.FieldSetSkin"
paddingLeft="10" >
<base:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle" gap="2" />
</base:layout>
<s:Scroller id="sor" verticalScrollPolicy="auto" horizontalScrollPolicy="off" width="100%" height="85%">
<s:VGroup id="group" width="100%" height="100%" gap="0" elementAdd="group_addedHandler(event)" enabled="{enabledFlag}" >
</s:VGroup>
</s:Scroller>
</base:FieldSet>
<s:HGroup height="15%">
<s:Button label="确定" click="button1_clickHandler(event)" enabled="{enabledFlag}" />
<s:Button label="取消" click="{this.visible=false}" />
</s:HGroup>
</s:TitleWindow>
转载于:https://blog.51cto.com/jiarong/1030335