Skip to content

Latest commit

 

History

History
79 lines (63 loc) · 1.99 KB

focusevent_and_buttonmode.md

File metadata and controls

79 lines (63 loc) · 1.99 KB

##FocusEvent与buttonMode的关系

这两个家伙有关系么?

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

它们之间的关系就是:

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

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

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

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

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.MouseEvent;
import flash.text.TextField;

public class Focus extends Sprite
{
	public function Focus()
	{
		getSprite();
		getSprite(200, 0);

		var _tf:TextField = new TextField();
		_tf.type = 'input';
		_tf.border = true;
		_tf.x = 200;
		_tf.y = 200;
		_tf.addEventListener(FocusEvent.FOCUS_IN, handler_focusin);
		_tf.addEventListener(FocusEvent.FOCUS_OUT, handler_focusout);
		addChild(_tf);

	}

	private var _sprite:Sprite;

	private function getSprite($x:int=0, $y:int=0):Sprite
	{
		var _sprite:Sprite = new Sprite();
		_sprite.graphics.beginFill(0);
		_sprite.graphics.drawRect(0,0,100,100);
		_sprite.graphics.endFill();
		_sprite.x = $x;
		_sprite.y = $y;
		_sprite.useHandCursor = true;
		_sprite.buttonMode = true;
		this.addChild(_sprite);
		_sprite.addEventListener(FocusEvent.FOCUS_OUT, handler_focusout);
		_sprite.addEventListener(FocusEvent.FOCUS_IN, handler_focusin);
		_sprite.addEventListener(MouseEvent.CLICK, handler_click);
		return _sprite;
	}

	private function handler_focusout(evt:FocusEvent):void
	{
		trace('focusout,target:', evt.target, ',relatedTarget:', evt.relatedObject);
	}

	private function handler_focusin(evt:FocusEvent):void
	{
		trace('focusin,target:', evt.target, ',relatedTarget:', evt.relatedObject);
	}

	private function handler_click(evt:MouseEvent):void
	{
		trace('click');
	}
}
}