Number Formatter: 00 not 0
I would like to format a number to display 00 instead of开发者_StackOverflow中文版 0, and 01, 02...10, 11,
function formatNumber(number) : String {
if (number > 10)
return number;
else
return '0' + number;
}
Perhaps this is presumptive of me but are you trying to represent seconds or minutes in some sort of time display? With Flex one way to do that would be to use the Date object and the DateFormatter. That way you can provide format strings to output your time however you want.
var seconds:int = 4;
var date:Date = new Date();
date.seconds = seconds;
var formatter:DateFormatter = new DateFormatter();
formatter.formatString = "SS";
trace(formatter.format(date));
You cannot format a number to have a leading 0 padding. But since you need this functionality to format the number display in a NumericStepper component, you can simply format the text in the output textField every time the value changes.
I wrote a little example for you. Create a new AS3 FLA, add a NumericStepper to the stage, set its instance name to "stepper". Then set Main.as as the main class.
Main.as:
package {
import fl.controls.NumericStepper;
import flash.text.TextField;
import flash.events.Event;
import flash.display.MovieClip;
public class Main extends MovieClip{
public var stepper:NumericStepper;
public function onStepperChange (ev:Event) : void {
updateStepper();
}
private function updateStepper() : void {
var num:String = stepper.value > 10 ? "" + stepper.value : "0" + stepper.value;
stepper.textField.text = num;
}
public function Main() {
updateStepper();
stepper.addEventListener (Event.CHANGE, onStepperChange);
}
}
}
If are using spark.components, then the NumericStepper has a valueFormatFunction property. If you are using fl.controls, then I think the solution above that uses Event.CHANGE on the NumericStepper is best.
This is how I would do it when using spark.components.NumericStepper
var num_fld:NumericStepper = new NumericStepper();
num_fld.valueFormatFunction = numericFieldFormat;
private function numericFieldFormat(num:Number) : String {
return((num < 10) ? '0' + String(num) : String(num));
}
if you are in Flex 3 you can't access directly to the inputField, but easy you can solve it by using mx_internal
<mx:NumericStepper id="stpprHours" minimum="1" maximum="24" stepSize="1" creationComplete="formatStepper(stpprHours)" change="stpprHours_changeHandler(event)" value="1"/>
<mx:NumericStepper id="stpprMinutes" minimum="0" maximum="59" stepSize="1" creationComplete="formatStepper(stpprMinutes)" change="stpprMinutes_changeHandler(event)" value="0"/>
and
<mx:Script>
<CDATA[[
protected function stpprHour_changeHandler(event:NumericStepperEvent):void {
formatStepper(stpprHours);
}
protected function stpprMinutes_changeHandler(event:NumericStepperEvent):void {
formatStepper(stpprMinute);
}
protected function formatStepper(stepper : NumericStepper) : void {
if(stepper.value<10) stepper.mx_internal::inputField.text = "0" + stepper.value;
}
]]>
</mx:Script>
精彩评论