+展开-XML
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.controls.TextInput;
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
[Bindable]
private var dp:ArrayCollection = new ArrayCollection([{name:"John Smith" , positionType:1}
, {name:"Ellen Smith" , positionType:2}
, {name:"James Smith" , positionType:3}
, {name:"Jane Smith" , positionType:4}]);
//下面定义的这个itemEditEnd 事件的事件侦听器,注意事件是一个包含reason 属性的ListEvent 对象,该属性将被用作判断用户是否修改了itemEditor 的值。
public function formatData(event:ListEvent):void {
// Check the reason for the event.
if(event.reason == "cancelled") {
// Do not update cell.
return;
}
// Get the new data value from the editor.
var newData:String = TextInput(event.currentTarget.itemEditorInstance).text;
// Determine if the new value is an empty String.
var reg:RegExp = //d/;
if(newData == ""|| reg.test(newData)) {
// Prevent the user from removing focus,
// and leave the cell editor open.
event.preventDefault();
// Use the errorString to inform the user that something is wrong.
TextInput(listInstance.itemEditorInstance).setStyle("borderColor",0xff0000);
TextInput(listInstance.itemEditorInstance).errorString = "Enter a valid string." ;
return void ;
}
//test for FirstName LastName format
reg = //w+./s./w+/
if (!reg.test(newData)) {
event.preventDefault();
TextInput( listInstance.itemEditorInstance).setStyle( "borderColor", 0xff0000);
TextInput(listInstance.itemEditorInstance).errorString = "Enter first name and last name";
} else{
//make sure the name is properly formatted
var firstName:String = newData.substring(0, newData.indexOf(" " ));
var lastName:String = newData.substring(newData.indexOf(" " )+1);
firstName = firstName.charAt(0).toUpperCase() + firstName.substr(1);
lastName = lastName.charAt(0).toUpperCase() + lastName.substr(1);
TextInput(listInstance.itemEditorInstance).text = firstName+" " +lastName;
newData = newData.charAt(0).toLocaleUpperCase()+ newData.substring(1, newData.indexOf(" "
))+ newData.charAt(newData.indexOf(" " )+1)+ newData.substring(newData.indexOf(" " )+2);
}
}
]]>
</mx:Script>
<mx:List id="listInstance" dataProvider="{dp}" editable="true" labelField="name" itemEditEnd="formatData(event)" width="300"/>
</mx:Canvas>