If you don't have a lot of space for a label next to a TextInput, then you may want to try using my TextInputPrompted class.
You use this class just like you would any other TextInput, but there's an extra prompt property. Whatever you set prompt to is displayed in the TextInput in light grey. Once you start typing something in the TextInput, the prompt will go away, and the text turns dark.
View a demo.
/**
* Copyright (c) 2008 Geoffrey T. Bell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package
{
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import mx.controls.TextInput;
public class TextInputPrompted extends TextInput
{
private var __prompt:String = null;
private var __firstTime:Boolean = true;
/**
* Getter for prompt
*/
public function get prompt():String
{
return __prompt;
}
/**
* Setter for prompt
*/
public function set prompt( prompt:String ):void
{
__prompt = prompt;
showPrompt();
}
/**
* Getter for text
*/
override public function get text():String
{
return super.text;
}
/**
* Setter for text
*/
override public function set text( text:String ):void
{
hidePrompt();
super.text = text;
}
/**
* Constructor
*/
public function TextInputPrompted()
{
super();
if (prompt != null && prompt != "")
{
showPrompt();
}
// Event listeners
this.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
this.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
}
private function showPrompt():void
{
// If promopt is set, add it to the text property and set style
if (prompt != null && prompt != "" && text.length <= 0)
{
super.text = prompt;
// Style
this.setStyle("color", "#AAAAAA");
}
}
private function hidePrompt():void
{
// Remove prompt from text string and set style to default
super.text = null;
// Style
this.setStyle("color", "#333333");
}
private function onFocusIn( e:FocusEvent ):void
{
// Add listener for keys so we can hide prompt
this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onFocusOut( e:FocusEvent ):void
{
// Remove listener for keys
this.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
// Show prompt if text is blank
if (super.text == null || super.text == "")
{
showPrompt();
}
__firstTime = true;
}
private function onKeyDown( e:KeyboardEvent ):void
{
// Remove prompt if keyCode is a valid text character
if ((e.keyCode >=48 && e.keyCode <=57) ||
(e.keyCode >=65 && e.keyCode <=90) ||
(e.keyCode >=96 && e.keyCode <=107) ||
(e.keyCode >=109 && e.keyCode <=111) ||
(e.keyCode >=186 && e.keyCode <=192) ||
(e.keyCode >=219 && e.keyCode <=222))
{
hidePrompt();
// Remove listener for keys
this.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
}
}
}
2 comments:
Wow, this is great. I'm going to implement this tonight.
There is only one but in your code. You need to add a MouseEvent.CLICK otherwise when you click inside the textInput is not hiding the prompt.
Kind regards,
Manos
Post a Comment