-
package org.phpz.controls
-
{
-
import flash.events.Event;
-
import flash.events.KeyboardEvent;
-
import flash.ui.Keyboard;
-
-
import mx.containers.HBox;
-
import mx.controls.Label;
-
import mx.controls.TextInput;
-
-
[IconFile("icons/IpInputBox.png")]
-
-
public class IpInputBox extends HBox
-
{
-
private const DOT1:uint = 110;
-
private const DOT2:uint = 190;
-
private const STR_NULL:String = '';
-
-
private var nullNow:Boolean = false;
-
-
private var ips:Array;
-
private var txt:Array;
-
-
public function IpInputBox()
-
{
-
super();
-
ips = new Array('','','','');
-
txt = new Array();
-
}
-
-
override protected function createChildren():void
-
{
-
super.createChildren();
-
setStyle('horizontalGap', 0);
-
for (var i:uint = 0; i <4; i++)
-
{
-
if (i> 0)
-
{
-
var newLabel:Label = new Label();
-
newLabel.text = '.';
-
newLabel.width = 7;
-
addChild(newLabel);
-
}
-
var newInput:TextInput = new TextInput();
-
newInput.maxChars = 3;
-
newInput.percentWidth = 100;
-
newInput.restrict = '0-9';
-
newInput.setStyle("textAlign", "center");
-
newInput.addEventListener(Event.CHANGE, changeHandler);
-
newInput.addEventListener(KeyboardEvent.KEY_UP, keyDownInTextHandler);
-
txt.push(newInput);
-
addChild(newInput);
-
}
-
}
-
-
//////////////////////////////
-
// getter & setter
-
//////////////////////////////
-
-
/**
-
* IP 值
-
* */
-
[Bindable]
-
public function set value(v:String):void
-
{
-
ips = v.split('.', 4);
-
for (var i:uint = 0; i <txt.length; i++)
-
{
-
txt[i].text = ips[i] = formatIpPart(ips[i]);
-
}
-
}
-
public function get value():String
-
{
-
for (var i:uint = 0; i <txt.length; i++)
-
{
-
txt[i].text = ips[i] = padIpPart(ips[i]);
-
}
-
return ips.join('.');
-
}
-
-
//////////////////////////////
-
// private functions
-
//////////////////////////////
-
-
/**
-
* 填补空位
-
* */
-
private function padIpPart(part:String):String
-
{
-
if (STR_NULL == part)
-
{
-
part = "0";
-
}
-
return part;
-
}
-
-
/**
-
* 格式化
-
* */
-
private function formatIpPart(part:String):String
-
{
-
if (null == part || STR_NULL == part)
-
{
-
part = '0';
-
}
-
else
-
{
-
part = part.replace(/[^0-9]/g, STR_NULL);
-
}
-
if (uint(part)> 255)
-
{
-
part = "255";
-
}
-
return part;
-
}
-
-
/**
-
* 使前一个输入框获得焦点
-
* */
-
private function setFocusToPrevText(target:TextInput):TextInput
-
{
-
var index:int = txt.indexOf(target);
-
if (index> 0)
-
{
-
txt[index - 1].setFocus();
-
txt[index - 1].selectionBeginIndex = txt[index - 1].selectionEndIndex = txt[index - 1].length;
-
return txt[index - 1];
-
}
-
return target;
-
}
-
-
/**
-
* 使后一个输入框获得焦点
-
* */
-
private function setFocusToNextText(target:TextInput):TextInput
-
{
-
var index:int = txt.indexOf(target);
-
if (index <3)
-
{
-
txt[index + 1].setFocus();
-
return txt[index + 1];
-
}
-
return target;
-
}
-
-
-
////////////////////////////
-
// handler
-
////////////////////////////
-
-
private function keyDownInTextHandler(event:KeyboardEvent):void
-
{
-
var thisText:TextInput = event.currentTarget as TextInput;
-
if (DOT1 == event.keyCode || DOT2 == event.keyCode)
-
{
-
STR_NULL != thisText.text && setFocusToNextText(thisText);
-
}
-
if (!nullNow && Keyboard.BACKSPACE == event.keyCode && 0 == thisText.selectionBeginIndex)
-
{
-
var curText:TextInput = setFocusToPrevText(thisText);
-
curText.text = curText.text.substr(0, curText.length - 1);
-
}
-
nullNow = false;
-
}
-
-
private function changeHandler(event:Event):void
-
{
-
var thisText:TextInput = event.currentTarget as TextInput;
-
var index:int = txt.indexOf(thisText);
-
thisText.text = ips[index] = formatIpPart(thisText.text);
-
if (thisText.length == 3)
-
{
-
// 焦点到下一输入框
-
setFocusToNextText(thisText);
-
}
-
if (STR_NULL == thisText.text)
-
{
-
nullNow = true;
-
}
-
}
-
} // end class
-
} // end package