Flex 3.0 中的NumericStepper中显示格式的问题
package com.XXX.utils
{
import flash.events.Event;
import flash.events.FocusEvent;
import flash.text.TextLineMetrics;
import mx.containers.HBox;
import mx.controls.Button;
import mx.controls.Label;
import mx.controls.NumericStepper;
import mx.controls.TextInput;
import mx.core.UITextField;
import mx.core.mx_internal;
import mx.events.FlexEvent;
import mx.formatters.DateFormatter;
import mx.managers.IFocusManager;
use namespace mx_internal;
/**
* Dispatched when the time changes, which could be either the hour, minute, or dayPart.
*/
[Event(name="change",type="flash.events.Event")]
/**
* Dispatched when the hour changes.
*/
[Event(name="hoursChange",type="flash.events.Event")]
public class NumberFormaterStepr extends NumericStepper
{
public function NumberFormaterStepr()
{
super();
this.addEventListener(FlexEvent.VALUE_COMMIT,valueCommandHandler);
}
/** * @private */
protected var TextInputField:TextInput; //重新做一个input框,遮盖原来的
/** * @private */ protected var _newvalue:Number = 0; //新的属性,用于存值
/** * @private */ private var _enabled:Boolean=true; //是否启用
override protected function createChildren():void//重写创建子对象方法
{
super.createChildren();
var widestNumber:Number=61;
var lineMetrics:TextLineMetrics = measureText(widestNumber.toString());
var textWidth:Number = lineMetrics.width + UITextField.TEXT_WIDTH_PADDING+4;
if (!TextInputField)
{
TextInputField = new TextInput();
TextInputField.tabEnabled=true;
TextInputField.focusEnabled = true;
TextInputField.styleName = this;
TextInputField.width=textWidth;
TextInputField.restrict="0-9";
TextInputField.text = formatNumberWithChar(_newvalue);
TextInputField.parentDrawsFocus = true;
TextInputField.setStyle("textAlign","right");
TextInputField.setStyle("borderStyle","solid");
TextInputField.setStyle("paddingLeft",0);
TextInputField.setStyle("paddingRight",0);
TextInputField.setStyle("paddingTop",0);
TextInputField.setStyle("paddingBottom",0);
TextInputField.setStyle("horizontalGap",0);
TextInputField.setStyle("borderThickness",0.5);
TextInputField.setStyle("borderColor","#cccccc");
TextInputField.addEventListener(FocusEvent.FOCUS_OUT, inputField_focusOutHandler);
this.addChild(TextInputField);
}
inputField=TextInputField;
}
/** * @private * * do for format number to string */
private function valueCommandHandler(event:FlexEvent):void{
//var v=this.value;
inputField.text=formatNumberWithChar(value);
if(inputField==TextInputField){
this.newvalue=value;
}
}
/** * @private * Remove the focus from the text field. */
override protected function focusInHandler(event:FocusEvent):void {
super.focusInHandler(event);
var fm:IFocusManager = focusManager; if (fm) fm.defaultButtonEnabled = false;
}
[Bindable]
/** * The hours (an integer from 0 to 23) of the day. * * @default 0 */
public function get newvalue():Number { return _newvalue; }
[Inspectable(defaultValue=0,category="General",name="Hours")]
public function set newvalue(val:Number):void {
_newvalue = val;
this.invalidateProperties();
//dispatchEvent(new Event("hoursChange"));
// dispatchEvent(new Event("change"));
}
override protected function commitProperties():void
{
super.commitProperties();
TextInputField.text = formatNumberWithChar(_newvalue);
}
/** * @private */
override public function set enabled(value:Boolean):void {
_enabled = value;
if(TextInputField){
TextInputField.enabled=value;
nextButton.enabled=value;
prevButton.enabled=value;
}
}
/** * @private */
override public function get enabled():Boolean {
return _enabled;
}
//把数字转换成字符的。
public function formatNumberWithChar(value:Number):String
{
var len:Number=2;
var str:String=new String(value);//转换成String
var arr:Array=super.stepSize.toString().split("\\.");
if(arr.length>1){
len= arr[1].toString().length;
}
str=value.toFixed(len);
return str;
}
/** * @private * Place the buttons to the right of the text field. */
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
var w:Number = nextButton.getExplicitOrMeasuredWidth();
var h:Number = Math.round(unscaledHeight / 2);
var h2:Number = unscaledHeight - h;
nextButton.x = unscaledWidth - w;
nextButton.y= 0;
nextButton.setActualSize(w, h2);
prevButton.x = unscaledWidth - w;
prevButton.y = unscaledHeight - h;
prevButton.setActualSize(w, h);
}
/** * @private */
private function inputField_focusOutHandler(event:FocusEvent):void {
inputField.text=formatNumberWithChar(Number(TextInputField.text));
if(inputField==TextInputField){
this.newvalue=value;
}
focusOutHandler(event);
// Send out a new FocusEvent because the TextInput eats the event
// Make sure that it does not bubble
dispatchEvent(new FocusEvent(event.type, false, false, event.relatedObject, event.shiftKey,event.keyCode));
}
}
}
当你输入9的时候,你的stepsize 设置的0.01,那么就会显示9.00,当你输入9.236的时候,四舍五入为9.24.
推荐阅读
-
PHP中显示格式化的用户输入
-
mysqlreport显示Com_中change_db占用比例高的问题的解决方法
-
jsp页面中显示word/excel格式的文档的方法
-
解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题
-
jsp页面中EL表达式被当成字符串处理不显示值问题的解决方法
-
Excel单列或多列设置条件格式中的公式的引用问题实例详解
-
C#中winform中panel重叠无法显示问题的解决
-
解决在pycharm中显示额外的 figure 窗口问题
-
WInforn中设置ZedGraph的焦点显示坐标格式化以及显示三个坐标数的解决办法
-
快速解决Vue项目在IE浏览器中显示空白的问题