Flex提供了兩種創建自定義組件的方法。
- Using ActionScript
- Using MXML
Using ActionScript
可以通過擴展現有組件來創建組件。要使用Flash Builder創建組件,請單擊File>New>ActionScript Class。
輸入如下所示的詳細信息;
Flash Builder將創建以下CustomButton.as文件。
package com.tutorialspoint.client { import spark.components.Button; public class CustomButton extends Button { public function CustomButton() { super(); } } }
Using MXML
可以通過擴展現有組件來創建組件。若要使用Flash Builder創建組件,請單擊「新建MXML組件」。
輸入如下所示的詳細信息。
Flash Builder將創建以下CustomLogin.mxml文件。
<?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:Group>
讓我們按照以下步驟在Flex應用程式中測試自定義控制項−
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a package com.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Create CustomLogin.mxml and CustomButton.as component as explained above. Modify these files as explained below. Keep rest of the files unchanged. |
4 | Compile and run the application to make sure business logic is working as per the requirements. |
<?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:Form> <s:FormItem label = "UserName:"> <s:TextInput width = "200" /> </s:FormItem> <s:FormItem label = "Password:"> <s:TextInput width = "200" displayAsPassword = "true" /> </s:FormItem> <s:FormItem> <s:Button label = "Login" /> </s:FormItem> </s:Form> </s:Group>
package com.tutorialspoint.client { import spark.components.Button; public class CustomButton extends Button { public function CustomButton() { super(); this.setStyle("color","green"); this.label = "Submit"; } } }
<?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:client = "com.tutorialspoint.client.*" initialize = "application_initializeHandler(event)"> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <fx:Script> <![CDATA[ import mx.events.FlexEvent; protected function application_initializeHandler(event:FlexEvent):void { //create a new custom button var customButton: CustomButton = new CustomButton(); asPanel.addElement(customButton); } ]]> </fx:Script> <s:BorderContainer width = "630" height = "480" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "10" horizontalAlign = "center" verticalAlign = "middle"> <s:Label id = "lblHeader" text = "Custom Controls Demonstration" fontSize = "40" color = "0x777777" styleName = "heading" /> <s:Panel title = "Using MXML Component" width = "400" height = "200"> <client:CustomLogin> </client:CustomLogin> </s:Panel> <s:Panel title = "Using AS Component" width = "400" height = "100"> <s:VGroup id = "asPanel" width = "100%" height = "100%" gap = "10" horizontalAlign = "center" verticalAlign = "middle"> </s:VGroup> </s:Panel> </s:VGroup> </s:BorderContainer> </s:Application>
完成所有更改後,讓我們以正常模式編譯並運行應用程式,就像我們在flex-create application一章中所做的那樣。如果您的應用程式一切正常,它將產生以下結果:[聯機試用]