FocusEvent与buttonMode的关系

这两个家伙有关系么?

当然有,而且还挺紧密的。

它们之间的关系就是:

如果一个Sprite不开启buttonMode,那么就不会有FocusEvent事件发出

当然,TextField虽然没有buttonMode,也会有FocusEvent。

那么SimpleButton会如何?我没试,懒得试了……

把下面的代码中的 _sprite.buttonMode = true; 一行注释掉,可以看到(或者说看不到?)效果。

 1package
 2{
 3import flash.display.Sprite;
 4import flash.events.Event;
 5import flash.events.FocusEvent;
 6import flash.events.MouseEvent;
 7import flash.text.TextField;
 8
 9public class Focus extends Sprite
10{
11	public function Focus()
12	{
13		getSprite();
14		getSprite(200, 0);
15
16		var _tf:TextField = new TextField();
17		_tf.type = 'input';
18		_tf.border = true;
19		_tf.x = 200;
20		_tf.y = 200;
21		_tf.addEventListener(FocusEvent.FOCUS_IN, handler_focusin);
22		_tf.addEventListener(FocusEvent.FOCUS_OUT, handler_focusout);
23		addChild(_tf);
24
25	}
26
27	private var _sprite:Sprite;
28
29	private function getSprite($x:int=0, $y:int=0):Sprite
30	{
31		var _sprite:Sprite = new Sprite();
32		_sprite.graphics.beginFill(0);
33		_sprite.graphics.drawRect(0,0,100,100);
34		_sprite.graphics.endFill();
35		_sprite.x = $x;
36		_sprite.y = $y;
37		_sprite.useHandCursor = true;
38		_sprite.buttonMode = true;
39		this.addChild(_sprite);
40		_sprite.addEventListener(FocusEvent.FOCUS_OUT, handler_focusout);
41		_sprite.addEventListener(FocusEvent.FOCUS_IN, handler_focusin);
42		_sprite.addEventListener(MouseEvent.CLICK, handler_click);
43		return _sprite;
44	}
45
46	private function handler_focusout(evt:FocusEvent):void
47	{
48		trace('focusout,target:', evt.target, ',relatedTarget:', evt.relatedObject);
49	}
50
51	private function handler_focusin(evt:FocusEvent):void
52	{
53		trace('focusin,target:', evt.target, ',relatedTarget:', evt.relatedObject);
54	}
55
56	private function handler_click(evt:MouseEvent):void
57	{
58		trace('click');
59	}
60}
61}