2013-04-23 39 views
0

我試圖根據用戶輸入將輸入文本行爲從「單行」更改爲「密碼」,並返回,以便當用戶輸入不正確的密碼時文本字段將變爲單行文本框,其文本爲「錯誤的密碼」。然後,一旦他們開始再次打字,文本框將再次表現爲密碼類型文本框。在ActionScript 3中如何動態更改文本框的行爲

回答

2

下面是你想做的事情,它會在TextField中顯示Incorrect Password,直到輸入正確的密碼。

package 
{ 
    public class Main extends Sprite 
    { 
     private var tf:TextField = new TextField(); 

     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 
     } 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 


      tf.border = true; 
      tf.displayAsPassword = true; 
      tf.multiline = false; 
      tf.height = 20; 
      tf.type = TextFieldType.INPUT; 
      tf.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); 
      tf.addEventListener(FocusEvent.FOCUS_IN, onFocusIn); 

      addChild(tf); 

     } 

     private function onFocusIn(e:FocusEvent):void 
     { 
      tf.text = ""; 
      tf.displayAsPassword = true; 
     } 

     private function onKeyDown(e:KeyboardEvent):void 
     { 
      if (e.keyCode == Keyboard.ENTER) 
      { 
       if (tf.text == "pass") 
       { 
        trace("logged in"); 
       } 
       else 
       { 
        tf.displayAsPassword = false; 
        tf.text = "Incorrect Password"; 
        stage.focus = stage; 
       } 
      } 
     } 

    } 

} 
相關問題