需要该示例在FLEX应用程序中使用自定义组件

时间:2011-12-22 10:37:16

标签: actionscript-3 flex flex3

我想知道如何创建CustomComponent以及如何在FLEX Application中使用它。 给我一个简单的例子,我有一点知识,所以我可以理解你是否只是给我代码。我正在使用FLEX 3.0

提前致谢。

1 个答案:

答案 0 :(得分:0)

很高兴再次见到你。

格式化的Stepper.as

package component
{
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.MouseEvent;

import mx.controls.NumericStepper;
import mx.core.mx_internal;
import mx.events.FlexEvent;
import mx.formatters.Formatter;
use namespace mx_internal;

public class FormattedStepper extends NumericStepper {
    // formatter
    private var _formatter:Formatter;

    public function FormattedStepper() {
        super();
    }

    override protected function createChildren():void {
        super.createChildren();

        // un-restrict text input and set initial format
        mx_internal::inputField.restrict = null;
        doFormat();

        // event listeners
        addEventListener(FlexEvent.VALUE_COMMIT, doFormat);
        addEventListener(FocusEvent.FOCUS_OUT, doFormat);
        addEventListener(MouseEvent.CLICK, doFormat);
        addEventListener(FlexEvent.DATA_CHANGE, doFormat);
        addEventListener(MouseEvent.MOUSE_DOWN, doFormat);
        mx_internal::nextButton.addEventListener(MouseEvent.CLICK, doFormat);
        mx_internal::nextButton.addEventListener(MouseEvent.MOUSE_DOWN, doFormat);
        mx_internal::prevButton.addEventListener(MouseEvent.CLICK, doFormat);
        mx_internal::prevButton.addEventListener(MouseEvent.MOUSE_DOWN, doFormat);
        mx_internal::inputField.addEventListener(MouseEvent.CLICK, unFormat);
    }       

    private function doFormat(event:Event = null):void 
    {
        if ((_formatter != null) && (mx_internal::inputField != null) && (mx_internal::inputField.text != null)) 
        {
             mx_internal::inputField.text = _formatter.format(value);
        } 
    }

    private function unFormat(event:Event = null):void
     {
        if ((_formatter != null) && (mx_internal::inputField != null) && (mx_internal::inputField.text != null))
        {
             mx_internal::inputField.text = String(value);
        }            
    }

    public function set formatter(formatter:Formatter):void {
        _formatter = formatter;
        doFormat();
    }
}
}

Test.mxml

<mx:NumberFormatter id="numberFormatter" precision="2" useNegativeSign="true" rounding="none"/>

<mx:FormItem id="formitemLowerTolerance" label="Lower Tolerance:" labelStyleName="labelFormStyle" direction="horizontal">
            <component:FormattedStepper id="numstepLowerTolerance" width="150" formatter="{numberFormatter}"/>
            <mx:Label id="lblLower" text="%"/>
        </mx:FormItem>