package
{
import flash.events.Event;
import flash.events.FocusEvent;
import mx.controls.TextInput;
public class PromptingTextInput extends TextInput
{
private var _textEmpty:Boolean;
private var _currentlyFocused:Boolean = false;
public function PromptingTextInput()
{
_textEmpty = true;
addEventListener(Event.CHANGE, handleChange);
addEventListener(FocusEvent.FOCUS_IN, handleFocusIn);
addEventListener(FocusEvent.FOCUS_OUT, handleFocusOut);
}
private var _prompt:String = "";
public function get prompt():String {
return _prompt;
}
[Bindable]
public function set prompt(value:String):void {
_prompt = value;
invalidateProperties();
}
private var _promptFormat:String = '<font color="#999999"><i>[prompt]</i></font>';
public function get promptFormat():String {
return _prompt;
}
public function set promptFormat(value:String):void {
_promptFormat = value;
if (_promptFormat.indexOf("[prompt]") < 0) {
// TODO: Log error with the logging framework, or just use trace?
//trace( "PromptingTextInput warning: prompt format does not contain [prompt] replacement code." );
}
invalidateDisplayList();
}
private var _displayAsPassword:Boolean = false;
override public function set displayAsPassword(value:Boolean):void {
_displayAsPassword = value;
super.displayAsPassword = value;
}
override public function get displayAsPassword():Boolean {
return _displayAsPassword;
}
[Bindable('textChanged')]
[CollapseWhiteSpace]
[NonCommittingChangeEvent('change')]
override public function set text(value:String):void {
_textEmpty = (!value) || value.length == 0;
super.text = value;
invalidateDisplayList();
}
override public function get text():String {
if (_textEmpty) {
return '';
} else {
return super.text;
}
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
if (_textEmpty && _prompt !='' && !_currentlyFocused) {
if (_promptFormat == "") {
super.text = _prompt;
} else {
super.htmlText = _promptFormat.replace(//[prompt/]/g, _prompt);
}
if (super.displayAsPassword) {
var oldVal:Boolean = _displayAsPassword;
super.displayAsPassword = false;
_displayAsPassword = oldVal;
}
} else {
if (super.displayAsPassword != _displayAsPassword) {
super.displayAsPassword = _displayAsPassword;
}
}
super.updateDisplayList(unscaledWidth, unscaledHeight);
}
protected function handleChange(event:Event):void {
_textEmpty = super.text.length == 0;
}
protected function handleFocusIn(event:FocusEvent):void {
_currentlyFocused = true;
if (_textEmpty) {
super.htmlText = "";
validateNow();
}
}
protected function handleFocusOut(event:FocusEvent):void {
_currentlyFocused = false;
invalidateDisplayList();
}
}
}
例子:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white" xmlns:local="*">
<mx:XMLList id="statesXMLList">
<state abb="AL" name="Alabama" />
<state abb="AK" name="Alaska" />
<state abb="AZ" name="Arizona" />
<state abb="AR" name="Arkansas" />
<state abb="CA" name="California" />
<state abb="CO" name="Connecticut" />
</mx:XMLList>
<mx:ComboBox id="comboBox"
dataProvider="{statesXMLList}"
prompt="请选择"
rowCount="6"
labelField="@name"
itemRenderer="ComboBoxItemRenderer"/>
<local:PromptingTextInput width="225" id="aa" prompt="first" />
<local:PromptingTextInput id="password" width="225" prompt="Password" />
</mx:Application>