Flex设置TextInput的默认焦点、切换焦点的例子

1、首先,在加载swf的页面上写下下面的代码,控制页面将焦点设置到swf上:

在嵌入SWF的html里面加入如下的功能:
<body onload="document.getElementById('swf id').focus()">
这句话的含义:当onload完毕后,自动设定SWF为焦点状态。

2、接着,制作swf:

其中swf加载完毕后,立即设置焦点到指定的输入框,另外还有两个按钮可以将光标设置到指定的输入框中。
    <?xml version="1.0" encoding="utf-8"?>  
    <mx:Application name="FocusManager_setFocus_test"  
            xmlns:mx="http://www.adobe.com/2006/mxml"  
            layout="vertical"  
            verticalAlign="middle"  
            backgroundColor="white"  
            creationComplete="init()">  
      
        <mx:Script>  
            <![CDATA[ 
                import mx.controls.Alert; 
                 
                private function init():void { 
                    application.focusManager.setFocus(username); 
                } 
     
                private function submitButton_click(evt:MouseEvent):void { 
                    Alert.show(evt.currentTarget.label, 
                                Object(focusManager.getFocus()).name); 
                } 
     
                private function resetButton_click(evt:MouseEvent):void { 
                    username.text = ""; 
                    password.text = ""; 
                    Alert.show(evt.currentTarget.label, 
                                Object(focusManager.getFocus()).name); 
                } 
            ]]>  
        </mx:Script>  
      
        <mx:ApplicationControlBar dock="true">  
            <mx:Button label="Set focus to Username"  
                    click="focusManager.setFocus(username);" />  
            <mx:Button label="Set focus to Password"  
                    click="focusManager.setFocus(password);" />  
        </mx:ApplicationControlBar>  
      
        <mx:Form id="form"  
                defaultButton="{submitButton}">  
            <mx:FormItem label="Username:">  
                <mx:TextInput id="username" />  
            </mx:FormItem>  
            <mx:FormItem label="Password:">  
                <mx:TextInput id="password"  
                        displayAsPassword="true" />  
            </mx:FormItem>  
            <mx:FormItem direction="horizontal"  
                    horizontalAlign="right"  
                    width="100%">  
                <mx:Button id="submitButton"  
                        label="Submit"  
                        click="submitButton_click(event);" />  
                <mx:Button id="resetButton"  
                        label="Reset"  
                        click="resetButton_click(event);" />  
            </mx:FormItem>  
        </mx:Form>  
      
    </mx:Application>  

你可能感兴趣的:(html,xml,Flex)