【发布时间】:2015-05-31 00:39:51
【问题描述】:
当我运行此代码时,如果我单击“testExportToCSV”按钮,应用程序将按预期执行 [“将文件保存到...”窗口打开]。但是,一旦我单击该按钮,其他两个按钮就会以某种方式触发同一个窗口 ['save file to...'] 打开,就像我单击了“testExportToCSV”按钮一样。在调试应用程序时,我了解到其他两个按钮确实进入了正确的点击处理方法,尽管最终执行了函数'writeCSV(event:MouseEvent)',该函数在函数'WriteToOut()'中声明,它只是单击“testExportToCSV”按钮时调用。
请解释为什么会发生这种情况
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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="601"
backgroundColor="#3b5998"
creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.http.HTTPService;
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{}
/** ######## Click Handlers ######## */
protected function csvButton_clickHandler(event:MouseEvent):void{
trace("...inside csvButton_clickHandler(event:MouseEvent) ");
textBox.text = "In Export to csv method";
writeToOut();
}
protected function queryServer_clickHandler(event:MouseEvent):void{
trace("...inside queryServer_clickHandler(event:MouseEvent) ");
textBox.text = "In query server method";
serviceRequest.url = "https://msppluto.com/Planholders_Flex.php?customerId=15";
serviceRequest.send();
}
protected function refresh_clickHandler(event:MouseEvent):void{
trace("...inside refresh_clickHandler(event:MouseEvent) ");
textBox.text = "In refresh method";
validateNow();
}
/** ######## Write to ... ######## */
protected function writeToOut():void{
var myArray:Array = [];
for(var i:int = 0; i < 100; i++)
myArray[i] = {a:Math.random()*100, b:Math.random()*100, c:Math.random()};
var csv:String = '';
for(i = 0; i < 100; i++)
csv += myArray[i].a + ',' + myArray[i].b + ',' + myArray[i].c + '/n';
stage.doubleClickEnabled = true;
stage.addEventListener(MouseEvent.CLICK, writeCSV);
// why is this method being called when I click a different
// button ?????????????????????????????????????????
function writeCSV(event:MouseEvent):void{
var file:FileReference = new FileReference();
var bytes:ByteArray = new ByteArray();
bytes.writeUTFBytes(csv);
file.save(bytes, 'test.csv');
}
} // end writeToOut
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService
id="serviceRequest"
url="https://msppluto.com/Planholders_Flex.php"
useProxy="false">
</s:HTTPService>
</fx:Declarations>
<s:Label id="headline" text="HTTP Service" fontSize="45" x="10" y="10" color="#FFFFFF"></s:Label>
<s:Button id="testExportToCSV" x="14" y="187" minWidth="110" label="Export to csv" click="csvButton_clickHandler(event)"/>
<s:Button id="queryServerButton" x="14" y="216" minWidth="110" label="Query Server" click="queryServer_clickHandler(event)"/>
<s:Button id="refreshScreen" x="14" y="245" minWidth="110" label="refresh" click="refresh_clickHandler(event)"/>
<s:TextInput id="textBox" x="197" y="216" widthInChars="25" />
<mx:DataGrid id="dataGridForServiceRequest" x="575" y="160"
dataProvider="{serviceRequest.lastResult.planHolders.planHolder}">
<mx:columns>
<mx:DataGridColumn headerText="User ID" dataField="userid"/>
<mx:DataGridColumn headerText="User Name" dataField="username"/>
</mx:columns>
</mx:DataGrid>
【问题讨论】:
标签: actionscript-3 flash apache-flex button actionscript