Flash / Flex / ActionScript/TextField/focus event

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

change and scroll Event

 

package{
    import flash.text.TextField;
    import flash.events.*;
    import flash.display.Sprite;
    public class FocusIn extends Sprite{
        var primaryText:TextField;
        var secondaryText:TextField;
        public function FocusIn(){
            primaryText = new TextField();
            secondaryText = new TextField();
            primaryText.text = "This is the primary TextField.";
            secondaryText.text = "This is the secondary TextField";
            primaryText.addEventListener(FocusEvent.FOCUS_IN, setFocus);
            secondaryText.addEventListener(FocusEvent.FOCUS_IN, setFocus);
            primaryText.addEventListener(FocusEvent.FOCUS_OUT, loseFocus);
            secondaryText.addEventListener(FocusEvent.FOCUS_OUT, loseFocus);
            primaryText.background = true;
            secondaryText.background = true;
            primaryText.backgroundColor = 0xFFFFFF;
            secondaryText.backgroundColor = 0xFFFFFF;
            addChild(this.primaryText);
            addChild(this.secondaryText);
            secondaryText.x = 500;
        }
        private function setFocus(focus:FocusEvent):void
        {
            (focus.target as TextField).backgroundColor = 0xFF0000;
        }
        private function loseFocus(focus:FocusEvent):void
        {
            (focus.target as TextField).backgroundColor = 0xFFFFFF;
        }
    }
}



Responding When Text Is Selected or Deselected: field.addEventListener(FocusEvent.FOCUS_IN, onFocus);

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFieldType;
  import flash.events.FocusEvent;
  
  public class Main extends Sprite {
    private var _field1:TextField = new TextField(  );
    private var _field2:TextField = new TextField(  );
    public function Main(  ) {
      _field1.border = true;
      _field1.background = true;
      _field1.type = TextFieldType.INPUT;
      addChild(_field1);
      _field1.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, onKeyFocus);
      _field2.border = true;
      _field2.background = true;
      _field2.type = TextFieldType.INPUT;
      addChild(_field2);
      _field2.y = 100;
      
    }
    
    private function onKeyFocus(event:FocusEvent):void {
      if(_field1.text == "") {
          event.preventDefault(  );
      }
    }
  }
}