码迷,mamicode.com
首页 > 其他好文 > 详细

Flex的自定义事件(组件)的那些事儿

时间:2014-06-07 06:57:42      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

Demo:Login

1.创建一个UserLogin类。

bubuko.com,布布扣
package com
{   
    [Bindable]
    public class UserLogin
    {
        public var logUserNam:String=""; //用户名
        public var logUserPass:String=""; //用户密码
        public var logCompanyNam:String=""; //公司名称
        public function UserLogin(userNam:String, userPass:String, companyNam:String)
        {
            this.logUserNam=userNam;
            this.logUserPass=userPass;
            this.logCompanyNam=companyNam;
        }
    }
}
bubuko.com,布布扣

2.自定义事件MyEvent。

bubuko.com,布布扣
package com
{
    import flash.events.Event;
    public class MyCustomEvent extends Event
    {
        public static const TESTMYEVENT:String="testMyEvent"; //使用常量定义事件的触发方式名称
        [Bindable]
        public var userLog:UserLogin;
        public function MyCustomEvent(userLogin:UserLogin,type:String)
        {
            super(type);
            this.userLog=userLogin;
        }
    }
}
bubuko.com,布布扣

3.自定义组件Component。

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<s:Group 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="400" height="300"
         >
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <fx:Metadata>
        [Event(name="testMyEvent",type="com.MyCustomEvent")]
    </fx:Metadata>
    <fx:Script>
        <![CDATA[
            import com.MyCustomEvent;
            import com.UserLogin;
            protected function login(event:MouseEvent):void
            {   
                var userLog:UserLogin;
                var myEvent:MyCustomEvent;
                if(logName.text!="" && password.text!="")
                {   
                    userLog = new UserLogin(logName.text,password.text,company.text);
                    myEvent = new MyCustomEvent(userLog,"testMyEvent");
                    this.dispatchEvent(myEvent);
                }
            }
        ]]>
    </fx:Script>

    <s:Panel width="100%" height="100%">
        <s:Form>
            <s:FormItem label="LogName:">
                <s:TextInput id="logName"/>
            </s:FormItem>
            <s:FormItem label="password">
                <s:TextInput id="password"/>
            </s:FormItem>
            <s:FormItem label="company">
                <s:TextInput id="company"/>
            </s:FormItem>
            <s:FormItem>
                <s:Button id="btn" label="login" click="login(event)"/>
            </s:FormItem>
        </s:Form>
    </s:Panel>
</s:Group>
bubuko.com,布布扣

4.创建Login.mxml。

bubuko.com,布布扣
<?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"
               xmlns:com="*"
               minWidth="955" minHeight="600"
               >
    <fx:Script>
        <![CDATA[
            import com.MyCustomEvent;
            import com.UserLogin;
            import mx.controls.Alert;
            protected function component_testMyEventHandler(event:MyCustomEvent):void
            {
                var userLog:UserLogin = event.userLog;
                text.appendText(userLog.logUserNam+","+userLog.logCompanyNam);
            }

        ]]>
    </fx:Script>
    <fx:Metadata>
        [Event(name="testMyEvent",type="com.MyCustomEvent")]
    </fx:Metadata>
     <s:TextArea id="text" text="abc" width="500" height="100"/>
        <com:Component id="component" width="500" height="300" x="500" y="100"
                    testMyEvent="component_testMyEventHandler(event)">
     </com:Component>
</s:Application>
bubuko.com,布布扣

------完成。

Flex的自定义事件(组件)的那些事儿,布布扣,bubuko.com

Flex的自定义事件(组件)的那些事儿

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/zhangygl/p/3770675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!