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.
分享到:
相关推荐
Flex 3 Flex 2 技巧 Flex编译器支持条件编译 ...精通Flex 3.0――4.7 ActionScript 3.0网络编程 精通Flex 3.0――4.7.1 加载服务端数据 精通Flex 3.0――4.7.2 基于Socket的连接 精通Flex 3.0――4.8 使用事件处理类
Flex3.0面试题Flex3.0面试题Flex3.0面试题Flex3.0面试题Flex3.0面试题
Flex3.0学习版快速入门提高版,简单轻松,操作容易。学习简单,很清楚地描述了怎么从零凯斯学习Flex的开发。
精通flex3.0 精通 Flex3.0 LCDS ActionScript 事件
完全自学flex3.0随书源码,目前正在学,随后上传学习笔记及实例.
flex3.0资源flex3.0资源flex3.0资源flex3.0资源flex3.0资源
ArcGIS API for Flex 3.0离线文档
精通Flex 3.0--基于ActionScript 3.0实现源码
由于上传限制在15M,所以只有打包成四个……资源来自网络,仅供学习参考。 //[完全手册Flex3.0RIA开发.part2/4.rar
FLASH-FLEX3.0开发教程中文版(例子源码)
FLEX 3.0程序开发技术详解(下).pdf
flex3.0资源1flex3.0资源1flex3.0资源1
flex3.0 中文帮助文档 对于英语不太好的朋友是个不错的帮助哦!!
《精通Flex 3.0——基于ActionScript 3.0实现》一书源代码。 Flex 3.0 ActionScript 3.0源代码 Flex 3.0源代码。 --------------------------- 第1篇 Flex技术概述 第1章 Flex概述 3 1.1 Flex简介 3 1.2 Flex...
Flex3.0中文帮助文档part2.............
Flex ActionJscript 3.0中文帮助[pdf]
关于flex3.0的中英文学习资料,有兴趣的可以看看
Flex3.0中文帮助文档,欢迎下载使用
想学习flex技术的同学多多捧场 很实用的教程
ArcGIS Viewer for Flex 3.0源码