[AS3]as3的FocusEvent焦点事件的代码实例
一共有4个焦点事件:FocusIn、FocusOut、KEY_FOCUS_CHANGE、MOUSE_FOCUS_CHANGE,当焦点改变时focusIn和focusOut 事件会同时激活,它们属于不可取消的事件
一共有4个焦点事件:FocusIn、FocusOut、KEY_FOCUS_CHANGE、MOUSE_FOCUS_CHANGE,当焦点改变时focusIn和focusOut 事件会同时激活,它们属于不可取消的事件,keyFocusChange和mouseFocusChange是可取消的事件,可以调用FocusEvent.preventDefault( )方法取消默认事件,FocusEvent有一个relatedObject属性,对于focusIn而言,relatedObject属性是刚才拥有焦点的对象的;对于其它三个事件,relatedObject属性是现在接收到焦点的对象
- package {
- import flash.display.Sprite;
- import flash.events.FocusEvent;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFieldType;
- public class Sample0414 extends Sprite
- {
- private var lblInfo:TextField;
- public function Sample0414()
- {
- var textBox:TextField = new TextField();
- textBox.name = "文本框A";
- textBox.type = TextFieldType.INPUT;
- textBox.background = true;
- textBox.width = 200;
- textBox.height = 20;
- //textBox.addEventListener(FocusEvent.FOCUS_IN,onFocusIn);
- //textBox.addEventListener(FocusEvent.FOCUS_OUT,onFocusOut);
- textBox.addEventListener(FocusEvent.KEY_FOCUS_CHANGE,onKeyFocusChange);
- this.addChild(textBox);
- textBox = new TextField();
- textBox.name = "文本框B";
- textBox.type = TextFieldType.INPUT;
- textBox.background = true;
- textBox.width = 200;
- textBox.height = 20;
- textBox.y = 80;
- //textBox.addEventListener(FocusEvent.FOCUS_IN,onFocusIn);
- //textBox.addEventListener(FocusEvent.FOCUS_OUT,onFocusOut);
- textBox.addEventListener(FocusEvent.KEY_FOCUS_CHANGE,onKeyFocusChange);
- this.addChild(textBox);
- lblInfo = new TextField();
- lblInfo.y = 200;
- lblInfo.autoSize = TextFieldAutoSize.CENTER;
- this.addChild(lblInfo);
- }
- private function onFocusIn(event:FocusEvent):void
- {
- var txt:TextField = event.target as TextField;
- txttxt.text = txt.name;
- if(event.relatedObject!=null)
- {
- lblInfo.text = "CuPlayer.com提示FocusIn的relatedObject是:" + (event.relatedObject as TextField).name;
- }
- }
- private function onFocusOut(event:FocusEvent):void
- {
- if(event.relatedObject!=null)
- {
- lblInfo.text = "FocusOut的relatedObject是:" + (event.relatedObject as TextField).name;
- }
- }
- private function onKeyFocusChange(event:FocusEvent):void
- {
- var txt:TextField = event.target as TextField;
- if(txt.text == "")
- {
- event.preventDefault();//当field没有任何文字前,不允许使用Tab键把焦点移除
- }
- if(event.relatedObject!=null)
- {
- var strRelatedObj:String = (event.relatedObject as TextField).name;
- lblInfo.text = "CuPlayer.com提示KeyFocusChange的relatedObject是:" + strRelatedObj;
- }
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...