To create and use a custom formatter:
1.
In Flash, create a new ActionScript file.
2.
Add the following code to the file:
// NumberFormatter.as
class NumberFormatter extends mx.data.binding.CustomFormatter {
// Format a Number, return a String
function format(rawValue) {
var returnValue;
var strArray = new Array('one', 'two', 'three');
var numArray = new Array(1, 2, 3);
returnValue = 0;
for (var i = 0; i<strArray.length; i++) {
if (rawValue == numArray[i]) {
returnValue = strArray[i];
break;
}
}
return returnValue;
} // convert a formatted value, return a raw value
function unformat(formattedValue) {
var returnValue;
var strArray = new Array('one', 'two', 'three');
var numArray = new Array(1, 2, 3);
returnValue = "invalid";
for (var i = 0; i<strArray.length; i++) {
if (formattedValue == strArray[i]) {
returnValue = numArray[i];
break;
}
}
return returnValue;
}
}
3.
Save the ActionScript file as NumberFormatter.as.
4.
Create a new Flash (FLA) file.
5.
From the Components panel, drag a TextInput component to the Stage and name it
textInput. Then drag a NumericStepper component to the Stage and name it stepper.
6.
Open the Timeline and select the first frame on Layer 1.
7.
In the Actions panel, add the following code to the Actions panel:
import mx.data.binding.*;
var x:NumberFormatter;
var customBinding = new Binding({component:stepper, property:"value",
event:"change"}, {component:textInput, property:"text",
event:"enter,change"}, {cls:mx.data.formatters.Custom,
settings:{classname:"NumberFormatter"}});
The second line of code (
custom formatter class is included in the compiled SWF file.
var x:NumberFormatter
CustomFormatter class (Flash Professional only)
) ensures that the byte code for your
213
Need help?
Do you have a question about the FLASH 8-COMPONENTS LANGUAGE and is the answer not in the manual?
Questions and answers