Flex父子窗口相互調(diào)用實(shí)現(xiàn)思路及源碼
更新時(shí)間:2014年05月07日 10:31:38 作者:
這篇文章主要介紹了Flex父子窗口相互調(diào)用實(shí)現(xiàn)思路及源碼,需要的朋友可以參考下
1、設(shè)計(jì)思路
(1)子窗口調(diào)用父窗口的方法
(2)子窗口做了修改后,返回父窗口,父窗口調(diào)用子窗口函數(shù)
2、設(shè)計(jì)源碼
(1)父窗口
ParentWindow.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對象)放在此處 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
[Bindable]
//表格數(shù)據(jù)源綁定
private var grid:ArrayCollection = new ArrayCollection([
{number:"2014010101",name:"張散",sex:"男",age:"23"},
{number:"2014010102",name:"李思",sex:"女",age:"22"},
{number:"2014010101",name:"吳王",sex:"男",age:"21"},
{number:"2014010101",name:"趙柳",sex:"女",age:"20"},
{number:"2014010101",name:"游華",sex:"男",age:"22"},
{number:"2014010101",name:"祝思",sex:"女",age:"18"},
{number:"2014010101",name:"周禮",sex:"男",age:"19"},
{number:"2014010101",name:"華捷",sex:"女",age:"20"},
{number:"2014010101",name:"劉亮",sex:"男",age:"22"},
{number:"2014010101",name:"沈雪",sex:"女",age:"21"}
]);
/*修改事件函數(shù)*/
protected function updateHandler(event:MouseEvent):void
{
//新建子窗體對象
var childWindow:ChildWindow = new ChildWindow();
//將子窗體添加到PopUpManager中
PopUpManager.addPopUp(childWindow,this,true);
//向子窗體傳遞參數(shù)
childWindow.age = "23";
//子窗口調(diào)用父窗口函數(shù)
childWindow.callBack = this.myFunction;
//子窗體居中彈出
PopUpManager.centerPopUp(childWindow);
}
/*刷新函數(shù)*/
public function myFunction(you:String):void
{
Alert.show(you+"Hello");
}
]]>
</fx:Script>
<mx:VBox width="100%" height="100%" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
<mx:DataGrid id="dataGrid" dataProvider="{grid}" rowCount="{grid.length + 1}" width="100%"
verticalAlign="middle" textAlign="center">
<mx:columns>
<mx:DataGridColumn headerText="學(xué)號" dataField="number"/>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性別" dataField="sex"/>
<mx:DataGridColumn headerText="年齡" dataField="age"/>
</mx:columns>
</mx:DataGrid>
<mx:HBox width="100%" horizontalAlign="center" verticalAlign="middle">
<s:Button label="修改" click="updateHandler(event)"/>
</mx:HBox>
</mx:VBox>
</s:Application>
(2)子窗口
ChildWindow.mxml:
<?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:mx="library://ns.adobe.com/flex/mx" width="300" height="220"
close="closeHandler(event)" title="修改窗口">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
//回調(diào)函數(shù)
public var callBack:Function;
public var age:String = "";
/*關(guān)閉事件函數(shù)*/
protected function closeHandler(event:CloseEvent):void
{
PopUpManager.removePopUp(this);
}
/*修改按鈕事件函數(shù)*/
protected function updateHandler(event:MouseEvent):void
{
stuAge.text = age;
}
/*取消按鈕事件函數(shù)*/
protected function cancelHandler(event:MouseEvent):void
{
number.text = "";
stuName.text = "";
PopUpManager.removePopUp(this);
if(isPopUp)
{
callBack.call(parent);
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對象)放在此處 -->
</fx:Declarations>
<mx:VBox width="100%" height="100%">
<mx:Form>
<mx:FormItem label="學(xué)號:">
<s:TextInput id="number" width="200" maxChars="10"/>
</mx:FormItem>
<mx:FormItem label="姓名:">
<s:TextInput id="stuName" width="200" maxChars="10"/>
</mx:FormItem>
<mx:FormItem label="性別:">
<mx:HBox width="100%">
<mx:RadioButtonGroup id="sex"/>
<s:RadioButton groupName="sex" label="男" selected="true"/>
<s:RadioButton groupName="sex" label="女"/>
</mx:HBox>
</mx:FormItem>
<mx:FormItem label="年齡:">
<s:TextInput id="stuAge" width="200" maxChars="2"/>
</mx:FormItem>
<mx:FormItem>
<mx:HBox width="100%">
<s:Button label="修改" click="updateHandler(event)"/>
<s:Label width="42"/>
<s:Button label="取消" click="cancelHandler(event)"/>
</mx:HBox>
</mx:FormItem>
</mx:Form>
</mx:VBox>
</s:TitleWindow>
3、設(shè)計(jì)結(jié)果
(1)初始化時(shí)
(2)單擊修改按鈕
(1)子窗口調(diào)用父窗口的方法
(2)子窗口做了修改后,返回父窗口,父窗口調(diào)用子窗口函數(shù)
2、設(shè)計(jì)源碼
(1)父窗口
ParentWindow.mxml:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對象)放在此處 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
[Bindable]
//表格數(shù)據(jù)源綁定
private var grid:ArrayCollection = new ArrayCollection([
{number:"2014010101",name:"張散",sex:"男",age:"23"},
{number:"2014010102",name:"李思",sex:"女",age:"22"},
{number:"2014010101",name:"吳王",sex:"男",age:"21"},
{number:"2014010101",name:"趙柳",sex:"女",age:"20"},
{number:"2014010101",name:"游華",sex:"男",age:"22"},
{number:"2014010101",name:"祝思",sex:"女",age:"18"},
{number:"2014010101",name:"周禮",sex:"男",age:"19"},
{number:"2014010101",name:"華捷",sex:"女",age:"20"},
{number:"2014010101",name:"劉亮",sex:"男",age:"22"},
{number:"2014010101",name:"沈雪",sex:"女",age:"21"}
]);
/*修改事件函數(shù)*/
protected function updateHandler(event:MouseEvent):void
{
//新建子窗體對象
var childWindow:ChildWindow = new ChildWindow();
//將子窗體添加到PopUpManager中
PopUpManager.addPopUp(childWindow,this,true);
//向子窗體傳遞參數(shù)
childWindow.age = "23";
//子窗口調(diào)用父窗口函數(shù)
childWindow.callBack = this.myFunction;
//子窗體居中彈出
PopUpManager.centerPopUp(childWindow);
}
/*刷新函數(shù)*/
public function myFunction(you:String):void
{
Alert.show(you+"Hello");
}
]]>
</fx:Script>
<mx:VBox width="100%" height="100%" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10">
<mx:DataGrid id="dataGrid" dataProvider="{grid}" rowCount="{grid.length + 1}" width="100%"
verticalAlign="middle" textAlign="center">
<mx:columns>
<mx:DataGridColumn headerText="學(xué)號" dataField="number"/>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性別" dataField="sex"/>
<mx:DataGridColumn headerText="年齡" dataField="age"/>
</mx:columns>
</mx:DataGrid>
<mx:HBox width="100%" horizontalAlign="center" verticalAlign="middle">
<s:Button label="修改" click="updateHandler(event)"/>
</mx:HBox>
</mx:VBox>
</s:Application>
(2)子窗口
ChildWindow.mxml:
復(fù)制代碼 代碼如下:
<?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:mx="library://ns.adobe.com/flex/mx" width="300" height="220"
close="closeHandler(event)" title="修改窗口">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
//回調(diào)函數(shù)
public var callBack:Function;
public var age:String = "";
/*關(guān)閉事件函數(shù)*/
protected function closeHandler(event:CloseEvent):void
{
PopUpManager.removePopUp(this);
}
/*修改按鈕事件函數(shù)*/
protected function updateHandler(event:MouseEvent):void
{
stuAge.text = age;
}
/*取消按鈕事件函數(shù)*/
protected function cancelHandler(event:MouseEvent):void
{
number.text = "";
stuName.text = "";
PopUpManager.removePopUp(this);
if(isPopUp)
{
callBack.call(parent);
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 將非可視元素(例如服務(wù)、值對象)放在此處 -->
</fx:Declarations>
<mx:VBox width="100%" height="100%">
<mx:Form>
<mx:FormItem label="學(xué)號:">
<s:TextInput id="number" width="200" maxChars="10"/>
</mx:FormItem>
<mx:FormItem label="姓名:">
<s:TextInput id="stuName" width="200" maxChars="10"/>
</mx:FormItem>
<mx:FormItem label="性別:">
<mx:HBox width="100%">
<mx:RadioButtonGroup id="sex"/>
<s:RadioButton groupName="sex" label="男" selected="true"/>
<s:RadioButton groupName="sex" label="女"/>
</mx:HBox>
</mx:FormItem>
<mx:FormItem label="年齡:">
<s:TextInput id="stuAge" width="200" maxChars="2"/>
</mx:FormItem>
<mx:FormItem>
<mx:HBox width="100%">
<s:Button label="修改" click="updateHandler(event)"/>
<s:Label width="42"/>
<s:Button label="取消" click="cancelHandler(event)"/>
</mx:HBox>
</mx:FormItem>
</mx:Form>
</mx:VBox>
</s:TitleWindow>
3、設(shè)計(jì)結(jié)果
(1)初始化時(shí)

(2)單擊修改按鈕

相關(guān)文章
Flex 改變樹結(jié)點(diǎn)圖標(biāo)的2種方法介紹
本文為大家介紹兩種方法改變樹結(jié)點(diǎn)圖標(biāo):根據(jù)是否有子結(jié)點(diǎn)進(jìn)行改變、根據(jù)結(jié)點(diǎn)的屬性,靈活改變圖標(biāo),具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07Flex上傳本地圖片并提前瀏覽的實(shí)現(xiàn)方法
個(gè)性頭像最終需要上傳到服務(wù)器的文件系統(tǒng)中,但是程序希望在用戶選擇后直接有個(gè)預(yù)覽,針對這個(gè)問題,下面有個(gè)不粗的實(shí)現(xiàn),希望對大家有所幫助2014-01-01Flex中通過RadioButton進(jìn)行切換示例代碼
這篇文章主要介紹了Flex中通過RadioButton進(jìn)行切換示例代碼,需要的朋友可以參考下2014-02-02flex4 panel去掉標(biāo)題設(shè)置透明度效果代碼
首先:去掉Panel的標(biāo)題,其次:設(shè)置透明度這個(gè)說了也是啰嗦,大家都會,不過還是提一下吧,具體請祥看本文2013-05-05