黑夜愁客 2009-03-20 20:38
浏览 317
已采纳

Flex界面之间的参数传递

我想做一个用户登入界面,正确登入后,要拿到刚才用户登入时输入的用户名。
主界面: APPlication.mxml
[code="xml"]<?xml version="1.0" encoding="utf-8"?>
creationComplete="initFrame()">
mx:Script
<![CDATA[
import mx.controls.Alert;
public function initFrame():void{
(new User()).showModal();

         }

     ]]>
 </mx:Script>

/mx:Application[/code]
登入框:User.mxml
[code="xml"]<?xml version="1.0" encoding="utf-8"?>
xmlns:flexmdi="flexmdi.containers.*" title="会员登入" layout="absolute" width="336" height="232"
minButtonVisible="false" maxButtonVisible="false" closeButtonVisible="false" creationComplete="initUser()" fontSize="20" borderColor="#FF0000">
mx:Script
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.CloseEvent
public function initUser():void {

         }
         public function checkUser():void {
             if(uname.text=="" || upass.text==""){
                 Alert.show("请输入用户名和密码",null,1);
             }else if(uname.text == upass.text){
             // do something and then close the login frame
                closeWindow();
               // (new MemberLogin()).showModal();
             } else {
                 Alert.show("用户名或者密码不正确", null, 1);
             }    
         }
         public function isQuit():void{
            Alert.show("Seriously? Close it?", null, 3, null, handleAlertResponse);
         }
        public function handleAlertResponse(event:CloseEvent):void
        {
            if(event.detail == mx.controls.Alert.YES)
            {
                closeWindow();

            }
        }
      ]]>
 </mx:Script>
<flexmdi:MDICanvas id="mdiCanvas" horizontalScrollPolicy="off" verticalScrollPolicy="off"
        width="296" height="168" backgroundColor="#FFFFFF" backgroundAlpha="0" x="10" y="10">
    <mx:Label x="23" y="28" text="会员号码:" width="97" fontSize="16"/>
    <mx:Label x="23" y="75" text="密码:" fontSize="16" width="74"/>
    <mx:TextInput id="uname" x="128" y="26" fontSize="16"/>
    <mx:TextInput id="upass" x="128" y="73" fontSize="16" fontFamily="Times New Roman" width="160"/>
    <mx:Button id="uexit" x="160" y="130" label="放弃" click="isQuit()" themeColor="#18FF00" fontSize="16"/>
    <mx:Button x="50" y="130" label="登入" click="checkUser()" themeColor="#18FF00" fontSize="16"/>
</flexmdi:MDICanvas>


[/code]

我想在主界面中拿到用户输入的用户名,这里是User.mxml中的uname这个的Text值。
所以我想知道怎么样才可以把Text值传到 APPlication.mxml中。
可以的话,麻烦你举个例子啊,谢谢

  • 写回答

1条回答 默认 最新

  • zenggang2008 2009-03-21 14:36
    关注

    通过自定义事件来解决
    自定义一个LoginEvent事件
    [code="java"]
    package events
    {
    import flash.events.Event;

    public class LoginEvent extends Event
    {
        public var username:String;
        public var password:String;
    
        public function LoginEvent(type:String, username:String, password:String)
        {
            super(type);
            this.username = username;
            this.password = password;
        }
    
    }
    

    }
    [/code]
    然后在登入框:User.mxml 中当登陆成功之后就dispatch自定义的事件,我这里帮你写个例子
    [code="xml"]
    <?xml version="1.0" encoding="utf-8"?>

    <mx:Metadata>
        [Event(name="login",type="events.LoginEvent")] 
    </mx:Metadata>
    <mx:Script>
        <![CDATA[
            import events.LoginEvent;
            private function clickHandler(event:Event):void
            {
                var e:LoginEvent = new LoginEvent("login",username.text,password.text);
                dispatchEvent(e);
            }
        ]]>
    </mx:Script>
    
    <mx:Label x="10" y="35" text="UserName:"/>
    <mx:Label x="10" y="61" text="Password:"/>
    <mx:TextInput x="97" y="33" id="username"/>
    <mx:TextInput x="97" y="59" displayAsPassword="true" id="password"/>
    <mx:Button x="97" y="100" label="Login" click="clickHandler(event)"/>
    

    /mx:Panel

    [/code]
    然后主的APPlication,mxml中就能监听到User.mxml发出的login事件
    [code="xml"]
    <?xml version="1.0" encoding="utf-8"?>

    mx:Script
    <![CDATA[

        import events.LoginEvent;
    
        import mx.controls.Alert;
    
        public function loginHandler(event:LoginEvent):void
        {  
    
            Alert.show("You logged in with username "+event.username,"Login Successful");
        }
    ]]>  
    

    /mx:Script

    /mx:Application
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)