flex 簡單例子(含實例效果圖 源碼)
更新時間:2009年09月29日 17:16:27 作者:
要過節(jié)了,工作任務也完成了,閑來沒事,研究svg,感覺市場上對svg支持度不高,導致了這項技術不能夠被IT群眾充分的接納,一個沒有人支持的技術,她必然也不會有很大的前景。研究來研究去,還是覺得flex比較好玩。
溫故而知新,簡簡單單的寫了一個例子。以后會在注圖形化處理方面多寫些例子.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" verticalAlign="top" applicationComplete="init()">
<mx:Script>
<!--[CDATA[
import mx.events.ItemClickEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
[Bindable]
public var dataColl:ArrayCollection=new ArrayCollection();
//ID編號
var k=1;
//聯系人對象
var userobj:Object;
//性別默認狀態(tài)
var sexobj:String="男";
//右鍵刪除菜單
var menucont:ContextMenu;
//初始化行數
var p:int=0;
public function init():void{
menucont=new ContextMenu();
addMenuItems();
}
//添加右鍵菜單項
public function addMenuItems():void{
var item:ContextMenuItem = new ContextMenuItem("刪除");
menucont.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemHandler);
menucont.hideBuiltInItems();
userdata.contextMenu = menucont;
}
//菜單右鍵事件處理
public function menuItemHandler(event:ContextMenuEvent):void{
var CaptionString:String=event.target.caption;
if(CaptionString=="刪除"){
//確定鼠標所在行數
var findex:Number = Math.floor(userdata.contentMouseY/userdata.rowHeight)-1 + userdata.verticalScrollPosition;
if (findex<0 || findex>=userdata.verticalScrollPosition+userdata.rowCount-1) //行號小于0時和大于datagrid行總數時的 錯誤處理
{
Alert.show("請選擇要刪除的行","提示");
} else {
p=findex;
dataColl.removeItemAt(p);
Alert.show("刪除成功","提示");
}
}
}
public function checkuser(event:MouseEvent):void{
var gname=uname.text;
var gphone=uphone.text;
if(gname!="" && gphone!=""){
dataprovider();
}else{
Alert.show("請輸入用戶名或電話號碼","提示");
}
}
//單選按鈕觸發(fā)事件
public function changsex(event:ItemClickEvent):void{
if(event.currentTarget.selectedValue=="f"){
sexobj= "女";
}else{
sexobj= "男";
}
}
//重置
public function resetvalue(event:MouseEvent):void{
uname.text="";
uphone.text="";
male.selected=true;
}
//創(chuàng)建聯系人信息
public function dataprovider():Object{
userobj=new Object();
userobj["uid"]=k;
userobj["uname"]=uname.text;
userobj["uphone"]=uphone.text;
userobj["usex"]=sexobj;
dataColl.addItem(userobj);
k++;
return userobj;
}
]]-->
</mx:Script>
<mx:Form>
<mx:Text text="聯系簿" fontWeight="bold" fontSize="14" textAlign="center" width="233" color="#1B78C6"/>
<mx:FormItem label="姓名" fontSize="12" fontWeight="bold" horizontalAlign="left">
<mx:TextInput id="uname"/>
</mx:FormItem>
<mx:RadioButtonGroup id="sexgroup" itemClick="changsex(event)"/>
<mx:FormItem label="性別" horizontalAlign="center" fontSize="12" fontWeight="bold">
<mx:HBox>
<mx:RadioButton id="male" label="男性" groupName="sexgroup" selected="true" value="m"/>
<mx:RadioButton id="female" label="女性" groupName="sexgroup" value="f"/>
</mx:HBox>
</mx:FormItem>
<mx:FormItem label="電話號碼" fontWeight="bold" fontSize="12" horizontalAlign="center">
<mx:TextInput id="uphone"/>
</mx:FormItem>
<mx:HBox width="232" height="35" verticalAlign="middle" horizontalAlign="center">
<mx:Button id="go" label="添加" fontSize="12" fontWeight="normal" click="checkuser(event)" textAlign="center" height="30" width="60"/>
<mx:Button id="reset" label="重置" fontWeight="normal" fontSize="12" height="30" width="60" click="resetvalue(event)"/>
</mx:HBox>
</mx:Form>
<mx:DataGrid dataProvider="{dataColl}" width="400" id="userdata"
fontSize="12" borderStyle="inset" fontWeight="bold" textAlign="center" alternatingItemColors="[#F1B6B6, #F2EB37]" height="210" editable="true">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="uid"/>
<mx:DataGridColumn headerText="聯系人" dataField="uname"/>
<mx:DataGridColumn headerText="性別" dataField="usex"/>
<mx:DataGridColumn headerText="電話號碼" dataField="uphone"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>

源碼:
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" verticalAlign="top" applicationComplete="init()">
<mx:Script>
<!--[CDATA[
import mx.events.ItemClickEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
[Bindable]
public var dataColl:ArrayCollection=new ArrayCollection();
//ID編號
var k=1;
//聯系人對象
var userobj:Object;
//性別默認狀態(tài)
var sexobj:String="男";
//右鍵刪除菜單
var menucont:ContextMenu;
//初始化行數
var p:int=0;
public function init():void{
menucont=new ContextMenu();
addMenuItems();
}
//添加右鍵菜單項
public function addMenuItems():void{
var item:ContextMenuItem = new ContextMenuItem("刪除");
menucont.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemHandler);
menucont.hideBuiltInItems();
userdata.contextMenu = menucont;
}
//菜單右鍵事件處理
public function menuItemHandler(event:ContextMenuEvent):void{
var CaptionString:String=event.target.caption;
if(CaptionString=="刪除"){
//確定鼠標所在行數
var findex:Number = Math.floor(userdata.contentMouseY/userdata.rowHeight)-1 + userdata.verticalScrollPosition;
if (findex<0 || findex>=userdata.verticalScrollPosition+userdata.rowCount-1) //行號小于0時和大于datagrid行總數時的 錯誤處理
{
Alert.show("請選擇要刪除的行","提示");
} else {
p=findex;
dataColl.removeItemAt(p);
Alert.show("刪除成功","提示");
}
}
}
public function checkuser(event:MouseEvent):void{
var gname=uname.text;
var gphone=uphone.text;
if(gname!="" && gphone!=""){
dataprovider();
}else{
Alert.show("請輸入用戶名或電話號碼","提示");
}
}
//單選按鈕觸發(fā)事件
public function changsex(event:ItemClickEvent):void{
if(event.currentTarget.selectedValue=="f"){
sexobj= "女";
}else{
sexobj= "男";
}
}
//重置
public function resetvalue(event:MouseEvent):void{
uname.text="";
uphone.text="";
male.selected=true;
}
//創(chuàng)建聯系人信息
public function dataprovider():Object{
userobj=new Object();
userobj["uid"]=k;
userobj["uname"]=uname.text;
userobj["uphone"]=uphone.text;
userobj["usex"]=sexobj;
dataColl.addItem(userobj);
k++;
return userobj;
}
]]-->
</mx:Script>
<mx:Form>
<mx:Text text="聯系簿" fontWeight="bold" fontSize="14" textAlign="center" width="233" color="#1B78C6"/>
<mx:FormItem label="姓名" fontSize="12" fontWeight="bold" horizontalAlign="left">
<mx:TextInput id="uname"/>
</mx:FormItem>
<mx:RadioButtonGroup id="sexgroup" itemClick="changsex(event)"/>
<mx:FormItem label="性別" horizontalAlign="center" fontSize="12" fontWeight="bold">
<mx:HBox>
<mx:RadioButton id="male" label="男性" groupName="sexgroup" selected="true" value="m"/>
<mx:RadioButton id="female" label="女性" groupName="sexgroup" value="f"/>
</mx:HBox>
</mx:FormItem>
<mx:FormItem label="電話號碼" fontWeight="bold" fontSize="12" horizontalAlign="center">
<mx:TextInput id="uphone"/>
</mx:FormItem>
<mx:HBox width="232" height="35" verticalAlign="middle" horizontalAlign="center">
<mx:Button id="go" label="添加" fontSize="12" fontWeight="normal" click="checkuser(event)" textAlign="center" height="30" width="60"/>
<mx:Button id="reset" label="重置" fontWeight="normal" fontSize="12" height="30" width="60" click="resetvalue(event)"/>
</mx:HBox>
</mx:Form>
<mx:DataGrid dataProvider="{dataColl}" width="400" id="userdata"
fontSize="12" borderStyle="inset" fontWeight="bold" textAlign="center" alternatingItemColors="[#F1B6B6, #F2EB37]" height="210" editable="true">
<mx:columns>
<mx:DataGridColumn headerText="ID" dataField="uid"/>
<mx:DataGridColumn headerText="聯系人" dataField="uname"/>
<mx:DataGridColumn headerText="性別" dataField="usex"/>
<mx:DataGridColumn headerText="電話號碼" dataField="uphone"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
相關文章
Flex與.NET互操作 了解FluorineFx的環(huán)境配置(遠程對象、網關、通道、目的地)
Flex中的遠程對象訪問,也就是服務端提供一個遠程服務對象(RemotingService Object),在Flex客戶端通過相應的訪問技術去調用遠程對象的過程。2009-06-06Flex與.NET互操作 使用HttpService、URLReqeust和URLLoader加載/傳輸數據
在前兩篇文章中分別介紹了Flex與.NET的WebService之間的數據交互通信知識,本文將介紹另外一種加載數據以及發(fā)起請求的方式。2009-06-06Flex 動態(tài)綁定BindingUtils.bindProperty
Flex 動態(tài)綁定BindingUtils.bindProperty實現代碼。2009-06-06Flex Gumbo中通過baseColor樣式 設置FxHScrollBar背景顏色
Gumbo中通過baseColor樣式 設置FxHScrollBar背景顏色的實現代碼。需要的朋友可以參考下。2009-08-08