Flex - 自定義控制元件



Flex 提供兩種建立自定義元件的方法。

  • 使用 ActionScript
  • 使用 MXML

使用 ActionScript

您可以透過擴充套件現有元件來建立元件。要在 Flash Builder 中建立元件,請單擊 **檔案 > 新建 > ActionScript 類**。

輸入如下所示的詳細資訊:

Flex ActionScript Component

Flash Builder 將建立以下 CustomButton.as 檔案。

package com.tutorialspoint.client {
   import spark.components.Button;

   public class CustomButton extends Button {
      public function CustomButton() {
         super();
      }
   }
}

使用 MXML

您可以透過擴充套件現有元件來建立元件。要在 Flash Builder 中建立元件,請單擊 **檔案 > 新建 > MXML 元件**。

輸入如下所示的詳細資訊。

Flex MXML Component

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 應用中測試自定義控制元件:

步驟 描述
1 建立一個名為 *HelloWorld* 的專案,位於 *com.tutorialspoint.client* 包下,如 *Flex - 建立應用* 章節中所述。
2 修改 *HelloWorld.mxml*,如下所述。保持其餘檔案不變。
3 建立 *CustomLogin.mxml* 和 *CustomButton.as* 元件,如上所述。修改這些檔案,如下所述。保持其餘檔案不變。
4 編譯並執行應用程式,以確保業務邏輯按要求工作。

以下是修改後的 mxml 檔案 **src/com.tutorialspoint/client/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: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>

以下是修改後的 mxml 檔案 **src/com.tutorialspoint/client/CustomButton.as** 的內容。

package com.tutorialspoint.client {
   import spark.components.Button;

   public class CustomButton extends Button {
      
      public function CustomButton() {
         super();
         this.setStyle("color","green");
         this.label = "Submit";
      }
   }
}

以下是修改後的 mxml 檔案 **src/com.tutorialspoint/client/HelloWorld.mxml** 的內容。

<?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 - 建立應用 章節中一樣,以普通模式編譯並執行應用程式。如果您的應用程式一切正常,它將產生以下結果:[ 線上試用 ]

Flex Custom Controls
廣告