Попробуйте следующий код: Это будет полезно для ввода формата ЧЧ: ММ 12ч без AM / PM
MXML-код:
<Timepicker maxChars="5" restrict="0-9" />
Код ActionScript:
package
{
import flash.events.KeyboardEvent;
import mx.controls.TextInput;
public class Timepicker extends TextInput
{
public function Timepicker()
{
}
override protected function keyUpHandler(event:KeyboardEvent):void
{
super.keyUpHandler(event);
if (text.length == 0)
{
return;
}
var keyDel:Boolean=false;
if (event.charCode == 8 || event.charCode == 46)
keyDel=true;
var numberString:String=text;
if (keyDel)
text=numberString;
if (numberString.length > 0 && !keyDel)
{
if (numberString.length == 2 && numberString.indexOf(":") == -1)
{
text=numberString;
textField.appendText(":");
}
else if (numberString.length >= 4 && Number(numberString.charAt(3)) > 5)
{
text=numberString.substr(0, 3)
}
else if (numberString.length == 3 && numberString.charAt(2) != ":")
{
if (Number(numberString.charAt(2)) <= 5)
{
var fourthDigit:String=numberString.charAt(2);
super.textField.text="";
super.textField.appendText(numberString.substring(0, 2) + ":" + fourthDigit);
}
else
{
super.textField.text="";
super.textField.appendText(numberString.substring(0, 2) + ":");
}
}
textField.setSelection(textField.length, textField.length);
}
}
override protected function keyDownHandler(event:KeyboardEvent):void
{
super.keyDownHandler(event);
var keyDel:Boolean=false;
if (event.charCode == 8 || event.charCode == 46)
keyDel=true;
super.text=text;
if (super.text.length == 0)
{
var inputVal:String=String.fromCharCode(event.charCode);
if (Number(inputVal) > 1)
{
super.textField.appendText("0" + inputVal + ":");
}
}
super.textField.setSelection(super.textField.length, super.textField.length);
}
}
}