Flex有提示功能的TextInput输入框,只需添加prompt即可实现。

文章来自:网易博客- 燕南侠客

写个类继承spark.components.TextInput,然后添加一个属性“prompt”,剩下的看代码很简单的。文本中蓝色部分是我自己写的。

package demo
{
  import flash.display.DisplayObject;
  import flash.events.FocusEvent;
  import spark.components.TextInput;
  public class TextInputProme extends TextInput
  {
    /**
      *  @private
      */
    private var _prompt:String = "";
    private var tmp:Boolean = false;
    /**
     *  prompt 作用是用于输入框提示。
     *  当输入框获得焦点时 清除 text的值和文本颜色,还原原来的文本颜色。
     *  当输入框失去焦点时,判断文本是否为空,如何为空继续显示提示,否则不做任何修改。
     *  
     *  @ 默认""
     *       
     *  @langversion 3.0
     *  @playerversion Flash 10
     *  @playerversion AIR 1.5
     *  @productversion Flex 4
     */
     public function get prompt():String
    {
       return _prompt;
    }
    /**
     *  @private
     */
    public function set prompt(value:String):void
    {
       if (_prompt == value)
       return;
       _prompt = value;
       super.text = _prompt;
       this.setStyle("color","#A9A8A8");
       tmp = true;
    }
  
    /**
     *  @private 当输入框获得焦点时
     */
    override protected function focusInHandler(event:FocusEvent):void
    {
       if (event.target == this)
       {
          // call setFocus on ourselves to pass focus to the
          // textDisplay.  This situation occurs when the
          // player occasionally takes over the first TAB
          // on a newly activated Window with nothing currently
          // in focus
          setFocus();
          return;
       }
       if(tmp){
          super.text = "";
          this.setStyle("color","#000000");
          tmp = false;
       }
       // Only editable text should have a focus ring.
       if (enabled && editable && focusManager)
       focusManager.showFocusIndicator = true;
   
       super.focusInHandler(event);
    }
  
    /**
     *  @private 当输入框失去焦点时
     */
    override  protected function focusOutHandler(event:FocusEvent):void
    {
        if(super.text == ""){
          tmp = true;
          super.text = _prompt;
          this.setStyle("color","#A9A8A8");
       }
        // We don't need to remove our event listeners here because we
     // won't receive keyboard events.
        if (isOurFocus(DisplayObject(event.target)))
       drawFocus(false);
    }
    public function TextInputProme()
    {
       super();
    }

  }
}

效果图:

posted @ 2013-01-06 11:38  学海无涯1999  阅读(780)  评论(0)    收藏  举报