Flex 自定義控件

2018-01-01 16:42 更新

Flex提供了兩種創(chuàng)建自定義組件的方法。

  • 使用ActionScript

  • 使用MXML

使用ActionScript

您可以通過擴(kuò)展現(xiàn)有組件來創(chuàng)建組件。 要使用Flash Builder創(chuàng)建組件,請單擊 File > New >ActionScript Class。 輸入詳細(xì)信息,如下所示。

Flex ActionScript Component

Flash Builder將創(chuàng)建以下CustomButton.as文件。

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

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

使用MXML

您可以通過擴(kuò)展現(xiàn)有組件來創(chuàng)建組件。 要使用Flash Builder創(chuàng)建組件,請單擊 File > New >MXML Component。 輸入詳細(xì)信息,如下所示。

Flex MXML Component

Flash Builder將創(chuàng)建以下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應(yīng)用程序中測試自定義控件:

步驟描述
1 Flex - 創(chuàng)建應(yīng)用程序章節(jié)中所述,在包 com.tutorialspoint.client 下創(chuàng)建名為 HelloWorld 的項目。
2修改 HelloWorld.mxml ,如下所述。 保持文件的其余部分不變。
4按上述說明創(chuàng)建 CustomLogin.mxml CustomButton.as 組件。 按以下說明修改這些文件。 保持文件的其余部分不變。
3編譯并運(yùn)行應(yīng)用程序,以確保業(yè)務(wù)邏輯按照要求工作。

以下是修改的mxml文件 src / com.tutorialspoint / client / Custom Login.mxml 的內(nèi)容。

<?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 / Custom Button.as 的內(nèi)容。

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 的內(nèi)容。

<?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>

準(zhǔn)備好所有更改后,讓我們以正常模式編譯和運(yùn)行應(yīng)用程序,就像在 Flex - 創(chuàng)建應(yīng)用程序中一樣 章節(jié)。 如果一切順利,您的應(yīng)用程序,這將產(chǎn)生以下結(jié)果:[在線試用]

Flex Custom Controls
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號