问题
I want to know that how to create a CustomComponent and how to use that in FLEX Application. Give me just a simple example, i have little bit of knowledge is there so i can understand if u just give me the code. I am using FLEX 3.0
Thanks in advance.
回答1:
Nice to see you again.
Formatted 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>
来源:https://stackoverflow.com/questions/8602644/need-the-example-to-use-custom-component-in-flex-application