⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maskedtextinput.as

📁 Sample CRUD flex with java
💻 AS
📖 第 1 页 / 共 3 页
字号:
     *  Check for the validity of the mask specified.
     *  If the mask is not valid then set the flag defaultBehaviour to true.
     *  If flag defaultBehaviour is set then the Masked Text Input Control will
     *  function as a standard Text Input control.
     */
	private function checkMask():void
	{
		if(!_inputMask || _inputMask == "")
		{
			defaultBehaviour = true;
			return ;
		}
		for(var i:int=0;i<_inputMask.length;i++)
		{	
			if((_inputMask.charAt(i) == ESCAPE && _inputMask.charAt(i+1) == ESCAPE)
			 || (_inputMask.charAt(i) == ESCAPE && isMask(_inputMask.charAt(i+1))))
			{
				i++;
				continue;
			}
			else if(_inputMask.charAt(i) == ESCAPE 
				&& (_inputMask.charAt(i+1) != ESCAPE || !isMask(_inputMask.charAt(i+1))))
			{
				defaultBehaviour = true;
				break;
			}
			else if(isMask(_inputMask.charAt(i)))
				continue;
		}
	}
	
	 /**
     *  @private
     *  Create child objects for displaying embeded hints.
     */
	override protected function createChildren():void
    {
    	super.createChildren();
    	
    	if(dateField)
    	{
	    	embedTextField = new UITextField();
			embedTextField.text = embedStr;
			addChildAt(embedTextField,getChildIndex(textField));
    	}
    }
	
	//--------------------------------------------------------------------------
    //
    //  Event Handlers
    //
    //--------------------------------------------------------------------------
	
	/**
	 *  @private
	 *  Handles cut and do nothing.
	 */
	private function menuHandler(event:Event):void
	{
		// In case of a standard text field, perform the default cut action
		if(defaultBehaviour)
		{
			return ;
		}
		var str:String = super.text;
		super.text = _working.join("");
		event.preventDefault();
		return;
	}

	/**
	 *  @private
	 *  Handles TAB and SHIFT+TAB event and repositions the insertion point.
	 *  The insertion point(cursor) is moved to the position of the
	 *  next/previous subfield respectively.
	 */
	 //commented to retain the default Tab and Shift-Tab behavior
	 //uncomment if you want the focus to be shifted to the
	 // next/previous sub-field in case of a Tab/Shift-Tab respectively.
	/* private function tabHandler(event:FocusEvent):void
	{	
		// Move the insertion point to the previous group of mask characters
		if(event.shiftKey && event.keyCode == Keyboard.TAB)
		{
				var foundMask:Boolean = false;
				if(!(_position <=0))
				{
					for(var i:int=_position; i >= 0; i--)
					{
						if(foundMask && (!maskMap[i][1] || i==0))
						{
							if(!maskMap[i][1])
							{
								_position = i + 1;
								setSelection(_position,_position);
								event.preventDefault();
								break;
							}
							if(i==0)
							{
								_position = 0;
								setSelection(_position,_position);
								event.preventDefault();
								break;
							}
						}
						
						if(!foundMask && (i>0 && i<actualLength) && !maskMap[i][1] && maskMap[i-1][1])
						{
							foundMask = true;
						}
						
					}
					changeSelection(_position);
				}
		}
		// Move the insertion point to the next group of mask characters
		else if(event.keyCode == Keyboard.TAB)
		{
			if(_position != actualLength-1)
			{
				for(i=_position; i < actualLength; i++)
				{
					if(!maskMap[i][1] && maskMap[i+1][1])
					{
						_position = i + 1;
						setSelection(_position,_position);
						changeSelection(_position);
						event.preventDefault();
						break;
					}
				}
			}
		}
	} */
	
	/**
	 *  @private
	 *  Handles MOUSE_CLICK event and repositions the insertion point.
	 *  The insertion point(cursor) is moved to the position of the
	 *  mouse click if all the characters in the subfield(to the left
	 *  of the click point) are filled.
	 */
	private function reposition( event:flash.events.MouseEvent ) : void
	{
		if(defaultBehaviour)
		{
			return ;
		}
		
		// Adding MOUSE_UP event listener
		if(event.type == MouseEvent.MOUSE_DOWN)
			event.preventDefault();

		// Handles triple click
		var now:Number = getTimer();
		if(lastTime != 0 && (now - lastTime) < 300)
		{
			lastTime = 0;
			setSelection(0,actualLength);
			event.preventDefault();
			return ;
		}
		
		// Changed so that cursor can not be positioned to a subfield
		// if any of the previous subfield is partially filled.
		//if(this.selectionBeginIndex <= actualLength 
			//	&& _working[this.selectionBeginIndex-1] == " ")
		if(this.selectionBeginIndex <= actualLength 
				&& isWorkingIncomplete(this.selectionEndIndex))
		{
			//if(maskMap[this.selectionBeginIndex-1][1])
			setSelection(_position,_position);
		}
		else
		{
			_position = this.selectionBeginIndex;
			if(focusByClick)
			{
				setSelection(_position,_position);
				focusByClick = false;
			}
		}
		event.preventDefault();
	}
	
	/**
	 *  @private
	 *  Handles MOUSE_DOUBLECLICK event and selects the sub field.
	 */
	private function handleDoubleClick(event:MouseEvent):void
	{
		if(defaultBehaviour)
			return ;
		
		textField.selectable = false;
		lastTime = getTimer();
		
		var startPos:int = textField.getCharIndexAtPoint(event.localX,event.localY) != -1 ?
							textField.getCharIndexAtPoint(event.localX,event.localY) : 
							(event.localX < 10 ? 0 : (actualLength -1) );
		
		while(startPos>0 && startPos<actualLength && maskMap[startPos-1][1])
			startPos--;
		
		if(!isWorkingIncomplete(startPos))
			changeSelection(startPos);
		
		event.stopImmediatePropagation();
		event.preventDefault();
		
		textField.selectable = true;
		
		return ;
	}
	
	/**
	 *  @private
	 *  Handles key press event for special keys like Delete, Backspace, etc.
	 */
	private function interceptKey( event:flash.events.KeyboardEvent ) : void
	{
		if(defaultBehaviour)
		{
			super.keyDownHandler(event);
			return ;
		}
		
		// Delete one character before the insertion point 
		// and moves the insertion point to one position back.
		if( event.keyCode == Keyboard.BACKSPACE ) {
			_position = selectionBeginIndex;
			handleDeletions(true);
		}
		// Delete one character at the insertion point 
		else if( event.keyCode == Keyboard.DELETE ) {
			handleDeletions();
		}
		// Moves the insertion point to the previous viable input position
		else if( event.keyCode == Keyboard.LEFT ) {
			if(_position >0 && _working[_position-1] == " ")
			{
				setSelection(_position,_position);
			}
			else
			{
			 	_position = this.selectionBeginIndex;
				retreatPosition();
			}
			event.preventDefault();
		}
		// Moves the insertion point to the next viable input position
		else if( event.keyCode == Keyboard.RIGHT ) {
			if(_position == actualLength -1)
			{
				++_position;
				setSelection(_position,_position);
			}
			else if(_position < actualLength && _working[_position] == " ")
			{
				setSelection(_position,_position);
			}
			else
				advancePosition(); 
				
			event.preventDefault();
		}
		// Moves the insertion point to the last viable input position
		else if( event.keyCode == Keyboard.END ) {
			var b:Boolean = false;
			for(var i:int = _position; i < actualLength; i++)
			{
				if(_working[i] == " ")
				{
					_position = i;
					setSelection(i,i);
					b = true;
					break;
				}
			}
			if(!b)
				_position = _working.length;
			event.preventDefault();
		}
		// Moves the insertion point to the first viable input position
		else if( event.keyCode == Keyboard.HOME ) {
			_position = -1;
			advancePosition(true);
		}
		workingUpdated = true;
		invalidateDisplayList();
	}
	
	/**
	 *  @private
	 *  Handle TEXT_INPUT events by matching the character with
	 *  the mask and either blocking or allowing the character.
	 */
	private function interceptChar( event:TextEvent ) : void
	{
		// If the mask is incorrect or not spceified then treat the control
		// as a standard TextInput control
		if(defaultBehaviour)
		{
			return ;
		}
		
		// Get the typed characters
		var input:String = event.text;
					
		if( _position >= actualLength ) {
			event.preventDefault();
			
			// If autoAdvance flag is set, then set the focus to the
			// next control according to the tab index.
			if(_autoAdvance && !isWorkingIncomplete())
			{
				var obj:UIComponent = UIComponent(focusManager.getNextFocusManagerComponent());
				if((obj is MaskedTextInput) || (obj is TextInput) || (obj is TextArea))
					obj.setFocus();
			}
			
			// Dispatch the inputMaskEnd Event
			dispatchEvent(new Event("inputMaskEnd"));
			return;
		}
		
		handleInput(input,event);
	}
	
	/**
	 *  @private
	 *  Consumes the FOCUS_IN and FOCUS_OUT event and repositions the insertion
	 *  point and perform additional checks based on the required property.
	 */
	private function interceptFocus( event:FocusEvent ) : void
	{
		if(defaultBehaviour)
		{
			return ;
		}
		
		if(event.type == FocusEvent.FOCUS_IN)
		{
			focusByClick = true;
			// If tab is used to move within the fields of the control,
			// then position the insertion point to the begining of the
			// next group of mask characters
			_position = -1;
				
			// advance the insertion point to the first viable input field.
			advancePosition();
			// selects the current subfield
			changeSelection(0);
		}
		else if(event.type == FocusEvent.FOCUS_OUT)
		{
			textField.setColor(0x000000);
			if(_required && isWorkingIncomplete())
			{
				var errorTextColor:Number = getStyle("errorTextColor");
		    	if(errorTextColor)
		    		textField.setColor(errorTextColor);
		    	else
		    		textField.setColor(0xFF0000);
			}
		}
		
	}
	
	/**
	 *  @private
	 *  Handles Deletion of characters caused by pressing
	 *  backspace/Delete key.
	 */
	private function handleDeletions(backSpace:Boolean = false):void
	{
		var i:int = 0;
		var s:String = "";
		if(this.selectionBeginIndex == this.selectionEndIndex || this.selectionBeginIndex == this.selectionEndIndex+1)
		{
			var startIndex:int = -1;
			var endIndex:int = actualLength;
			if(backSpace)
			{
				startIndex = 0;
				endIndex = actualLength + 1;
			}
				
			if(_position>startIndex && _position < endIndex)
			{
				if(backSpace)
					retreatPosition();
					
				i = _position;
				if(!maskMap[_position][1])
					_working[_position] = maskMap[_position][0];
				else
				{
					// Commented so that characters dont shift to left when deleting
					/* while((i+1) < actualLength && _working[i+1] != " " && maskMap[i+1][1])
					{
						_working[i] = _working[i+1];
						i++;
					} */
					_working[i] = " ";
					
					// Added so that delete can work fine.
					if(!backSpace && _position<actualLength-1 && _working[_position+1] != " ")
					{
						advancePosition();
						//setSelection(_position,_position);
					}
					
					if(dateField)
					{
						s = embedTextField.text == null ? "":embedTextField.text;
						if(s.length > 0)
						{
							s = s.substring(0,_position) + " " + s.substring(_position+1, s.length);
							s = s.substring(0,i) + embedStr.charAt(i) + s.substring(i+1, s.length);
							embedTextField.text = s;
						}
					}
				}
			}
		}
		else
		{
			_position = this.selectionBeginIndex;
			i = _position - 1;
			for(var j:int=this.selectionBeginIndex;j<this.selectionEndIndex;j++)
			{
				// Commented so that characters dont shift to left when deleting
				//i = _position;
				//if(!maskMap[_position][1])
				//	_working[_position] = maskMap[_position][0];
				i++;
				
				if(!maskMap[i][1])
					_working[i] = maskMap[i][0];
				else
				{
					// Commented so that characters dont shift to left when deleting
					/* while((i+1) < actualLength && _working[i+1] != " " && maskMap[i+1][1])
					{
						_working[i] = _working[i+1];
						i++;
					} */
					_working[i] = " ";
					
					if(dateField)
					{
						s = embedTextField.text == null ? "":embedTextField.text;
						if(s.length > 0)
						{
							// Commented so that characters dont shift to left when deleting
							//s = s.substring(0,_position) + " " + s.substring(_position+1, s.length);
							//s = s.substring(0,i) + embedStr.charAt(i) + s.substring(i+1, s.length);
							//embedTextField.text = s;
							
							s = s.substring(0,i) + " " + s.substring(i+1, s.length);
							s = s.substring(0,i) + embedStr.charAt(i) + s.substring(i+1, s.length);
							embedTextField.text = s;
						}
					}
				}
			}
		}
	}
	
	/**
	 *  @private
	 *  Change the selected text to select the text in the current subfield.
	 */
	
	private function changeSelection(pos:Number):void
	{
		var startPos:int = pos;
			
		while(!maskMap[startPos][1])
			startPos++;
		
		var endPos:int = startPos;
		
		while(endPos<actualLength && maskMap[endPos][1] && _working[endPos] !=" ")
			endPos++;
		setSelection(startPos,endPos);
	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -