Flash / Flex / ActionScript/Development/Focus Event — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 08:15, 26 мая 2010

Focus and Tab Events

 
package{
  import flash.display.*;
  import flash.events.*;
  public class Main extends Sprite
  {
        public function Main()
        {
            var spr:Sprite = new Sprite();
            var sprTwo:Sprite = new Sprite();
            spr.graphics.beginFill(0x00ff00, 1);
            spr.graphics.drawRect(0, 0, 100, 100);
            spr.graphics.endFill();
            sprTwo.graphics.beginFill(0x0000ff, 1);
            sprTwo.graphics.drawRect(0, 0, 100, 100);
            sprTwo.graphics.endFill();
            var btnOne:SimpleButton = new SimpleButton(spr, spr, spr, spr);
            var btnTwo:SimpleButton = new SimpleButton(sprTwo, sprTwo, sprTwo, sprTwo);
            addChild(btnOne);
            addChild(btnTwo);
            sprTwo.x = 300;
            btnOne.addEventListener(MouseEvent.MOUSE_OVER, focusMe);
            btnTwo.addEventListener(MouseEvent.MOUSE_OVER, focusMe);
            btnOne.addEventListener(FocusEvent.FOCUS_IN, fin);
            btnOne.addEventListener(FocusEvent.FOCUS_OUT, fout);
            btnTwo.addEventListener(FocusEvent.FOCUS_IN, fin);
            btnTwo.addEventListener(FocusEvent.FOCUS_OUT, fout);
        }
        private function fin(focusEvent:FocusEvent):void
        {
            trace(" focus in "+focusEvent.target+" related object "+focusEvent.relatedObject);
        }
        private function fout(focusEvent:FocusEvent):void
        {
            trace(" focus out "+focusEvent.target+" related object"+focusEvent.relatedObject);
        }
        private function focusMe(mouseEvent:MouseEvent):void
        {
            trace("stage focus ");
            stage.focus = (mouseEvent.target as InteractiveObject);
        }
    }
}



FocusEvent.KEY_FOCUS_CHANGE

 
package
{
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    public class Main extends Sprite
    {
        private var sprite:Sprite = new Sprite();
        public function Main()
        {
            sprite.graphics.beginFill(0xFF0000, 1);
            sprite.graphics.drawRect(0, 0, 20, 20);
            sprite.graphics.endFill();
            addChild(sprite);
            this.stage.addEventListener(KeyboardEvent.KEY_UP,moveSprite);
        }
        private function moveSprite(keyEvent:KeyboardEvent):void
        {
            switch (keyEvent.keyCode)
            {
                case 37:
                    sprite.x--;
                    break;
                case 38:
                    sprite.y--;
                    break;
                case 39:
                    sprite.x++
                    break;
                case 40:
                    sprite.y++;
                    break;
                default:
                    break;
            }
        }
    }
}



FocusEvent.MOUSE_FOCUS_CHANGE

 
package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.FocusEvent;
    public class Main extends Sprite
    {
        public function Main()
        {
            var tf1:TextField = new TextField();
            tf1.type = "input";
            tf1.height = 20;
            tf1.width = 100;
            tf1.border = true;
            addChild(tf1);
            tf1.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, checkFocus);
            var tf2:TextField = new TextField();
            tf2.type = "input";
            tf2.height = 20;
            tf2.width = 100;
            tf2.border = true;
            addChild(tf2);
            tf2.x = 200;
        }
        private function checkFocus(focusEvent:FocusEvent):void
        {
            if ((focusEvent.target as TextField).text == "")
            {
                focusEvent.preventDefault();
            }
        }
    }
}



focusIn and focusOut Events

 

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;
        }
    }
}



Handling focus events for a particular object

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  public class Main extends Sprite {
    private var namefield:TextField = new TextField(  );
    private var passfield:TextField = new TextField(  );
    public function Main (  ) {
      namefield.width      = 100;
      namefield.height     = 30;
      namefield.border     = true;
      namefield.background = true;
      namefield.type = TextFieldType.INPUT;
      passfield.width      = 100;
      passfield.height     = 30;
      passfield.y          = 50;
      passfield.border     = true;
      passfield.background = true;
      passfield.type = TextFieldType.INPUT;
      addChild(namefield);
      addChild(passfield);
      namefield.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE,
                                 focusChangeListener);
      namefield.addEventListener(FocusEvent.KEY_FOCUS_CHANGE,
                                 focusChangeListener);
    }
    private function focusChangeListener (e:FocusEvent):void {
      if (e.target == namefield && namefield.text.length < 3) {
        trace("Name entered is less than three characters long");
        e.preventDefault(  );
      }
    }
  }
}



Handling focus events globally

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  public class Main extends Sprite {
    public function Main (  ) {
      var field1:TextField = new TextField(  );
      field1.width      = 100;
      field1.height     = 30;
      field1.border     = true;
      field1.background = true;
      field1.type = TextFieldType.INPUT;
      var field2:TextField = new TextField(  );
      field2.width      = 100;
      field2.height     = 30;
      field2.y          = 50;
      field2.border     = true;
      field2.background = true;
      field2.type = TextFieldType.INPUT;
      addChild(field1);
      addChild(field2);
      stage.addEventListener(FocusEvent.FOCUS_IN, focusInListener);
    }
    private function focusInListener (e:FocusEvent):void {
      TextField(e.target).backgroundColor = 0xFF00FF00;
      if (e.relatedObject is TextField) {
        TextField(e.relatedObject).backgroundColor = 0xFFFFFFFF;
      }
    }
  }
}