您可以尝试以下方法:
- 在捕获阶段防止鼠标按下事件
- 无论如何都会发生 Itemclick 事件,因此请记住单击的项目索引并显示确认对话框
- 如果需要,请在 alet's close 处理程序中进行实际更改。
这是一个例子:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)"
>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.events.ListEvent;
[Bindable]
private var formDirty:Boolean = false;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
myList.addEventListener(MouseEvent.MOUSE_DOWN, myList_mouseDownHandler, true);
}
protected function myList_mouseDownHandler(event:MouseEvent):void
{
if (formDirty)
{
event.preventDefault();
event.stopImmediatePropagation();
}
}
protected function myList_itemClickHandler(event:ListEvent):void
{
if (formDirty)
{
var alert:Alert = Alert.show("Save changes?", "Confirm save", Alert.YES | Alert.NO | Alert.CANCEL, null, alertCloseHandler);
alert.data = event.rowIndex;
}
}
private function alertCloseHandler(evt:CloseEvent):void
{
var alert:Alert = evt.target as Alert;
switch (evt.detail)
{
case Alert.YES:
//Save changes
//
// Whatever....
//
case Alert.NO:
//manually change list index cahnge
// we also fall here from Alert.Yes case
formDirty = false;
myList.selectedIndex = alert.data as int;
break;
case Alert.CANCEL:
//Do notthing:
break;
}
}
protected function invalidateButton_clickHandler(event:MouseEvent):void
{
formDirty = true;
}
]]>
</mx:Script>
<mx:VBox>
<mx:List id="myList" itemClick="myList_itemClickHandler(event)">
<mx:dataProvider>
<mx:ArrayCollection>
<mx:String>Item 0</mx:String>
<mx:String>Item 1</mx:String>
<mx:String>Item 2</mx:String>
<mx:String>Item 3</mx:String>
<mx:String>Item 4</mx:String>
</mx:ArrayCollection>
</mx:dataProvider>
</mx:List>
<mx:Button id="invalidateButton" click="invalidateButton_clickHandler(event)" label="{formDirty?'Invalidated':'Invalidate form'}" enabled="{formDirty?false:true}"/>
</mx:VBox>