package { import fl.controls.Button; import flash.display.Graphics; import flash.display.Shape; import flash.display.Sprite; import flash.display.Stage; import flash.events.MouseEvent; import flash.filters.DropShadowFilter; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; /** * ALert信息提示类<br>使用时先调用init()方法初始化舞台对象,然后调用show()方法传入需要提示的信息 * @author hfy 2011-10-19 */ public class Alert { //舞台对象 private static var stage:Stage; /** * 第一次使用Alert类的时候,调用该方法,为Alert初始化舞台对象 * @param stageReference 舞台对象 */ public static function init(stageReference:Stage):void { stage = stageReference; } /** * 调用该方法使用Alert * @param text 需要显示的提示信息 */ public static function show(text:String):void { //如果舞台为空,那么退出Alert,不做任何操作 if (stage == null) return; //如果text为null,退出Alert if(text==null) return; //定义ALert的sprite面板 var myAlert:Sprite = new Sprite(); //添加遮罩层 myAlert.addChild(createMask()); //添加信息提示框主体 myAlert.addChild(createPrompt(text)); //为alert面板注册事件 assignListeners(myAlert,null); //加载显示 stage.addChild(myAlert) } /** * 为Alert注册事件 */ private static function assignListeners(myAlert:Sprite, callback:Function):void { var Prompt:Sprite = myAlert.getChildAt(1) as Sprite; var OKbutton:* = Prompt.getChildByName("sure"); // Cause Alert to call specified function when alert has been closed if (callback != null) { function myFunction(myEvent:MouseEvent):void { OKbutton.removeEventListener(MouseEvent.CLICK, myFunction); closeAlert(myEvent); callback(); }; OKbutton.addEventListener(MouseEvent.CLICK, myFunction); } else { OKbutton.addEventListener(MouseEvent.CLICK, closeAlert); } } /** * 关闭Alert的提示框 */ private static function closeAlert(event:MouseEvent):void { var button:* = event.target; button.removeEventListener(MouseEvent.CLICK, closeAlert); var prompt:* = event.target.parent; prompt.removeEventListener(MouseEvent.CLICK, doStartDrag); prompt.removeEventListener(MouseEvent.CLICK, doStopDrag); var myAlert:Sprite = event.target.parent.parent; stage.removeChild(myAlert); } /** * Creates the mask for the Alert */ private static function createMask():Sprite { var mask:Sprite=new Sprite; mask.graphics.lineStyle(2,0x0099ff); mask.graphics.beginFill(0xffffff); mask.graphics.drawRoundRect(0,0,stage.width,stage.height,5); mask.graphics.endFill(); mask.alpha=0.6; return mask; } /** * 创建一个提示框sprite位于舞台中央。并显示提示信息和确定按钮。 * @param text 提示信息 */ private static function createPrompt(text:String):Sprite { var holder:Sprite = new Sprite; holder.name="holder"; //注册提示框的拖拽事件 holder.addEventListener(MouseEvent.MOUSE_DOWN, doStartDrag); holder.addEventListener(MouseEvent.MOUSE_UP, doStopDrag); //创建提示信息文本框和确定按钮 var textField:TextField=getTextField(text); var button:Button=getSureButton(); //创建提示框holder的graphice样式 GraphicePrompt(holder,textField); //设置文本框和按钮的坐标位置,并加载到显示界面 textField.x=(holder.width/2)-(textField.width/2); textField.y=26; button.x=(holder.width/2)-(button.width/2); button.y=holder.height-27; holder.addChild(textField); holder.addChild(button); //设置holder的显示坐标位置 holder.x = (stage.stageWidth/2)-(holder.width/2); holder.y = (stage.stageHeight/2)-(holder.height/2); return holder; } /** * 绘制信息提示框主体的显示样式 * @param holder 提示框的主体对象 * @param text 提示信息文本框 */ private static function GraphicePrompt(holder:Sprite,text:TextField):void { var myWidth:int = text.width+50; var myHeight:int = text.height+60; if (myWidth < 150) { myWidth = 150; } if (myHeight < 80) { myHeight = 80; } if (myHeight > stage.stageHeight) { myHeight = stage.stageHeight - 20; text.autoSize = TextFieldAutoSize.NONE; text.height = stage.stageHeight-80; } //holder.graphics.lineStyle(1,0,0); holder.graphics.beginFill(0xffffff); holder.graphics.drawRoundRect(0,0,myWidth,myHeight,20); holder.graphics.endFill(); var shap1:Shape=new Shape; var shap2:Shape=new Shape; shap1.graphics.beginFill(0x768f9a); shap1.graphics.drawRoundRectComplex(0,0,myWidth,20,10,10,0,0); shap1.graphics.endFill(); shap2.graphics.beginFill(0x899fa9); shap2.graphics.drawRoundRectComplex(0,21,myWidth,myHeight-21,0,0,10,10); shap2.graphics.endFill(); holder.addChild(shap1); holder.addChild(shap2); var tip:TextField=getTextField("Alert",true); tip.x=5; tip.y=0; holder.addChild(tip); //设置阴影效果 var shadow:DropShadowFilter = new DropShadowFilter; shadow.distance = 5; shadow.alpha=0.5; holder.filters = [shadow]; } /** * 生成一个textfield文本框 * @param text 提示信息 */ private static function getTextField(Text:String,bold:Boolean=false):TextField { var myTextField:TextField = new TextField(); //计算文本字符的长度 var length:Number=CalLength(Text); if(length>300){ myTextField.width=300; } else{ myTextField.width=length; } //字体显示为白色 myTextField.textColor = 0xffffff; myTextField.name = "textfield"; //允许多行显示 myTextField.multiline = true; myTextField.wordWrap=true; myTextField.selectable = false; myTextField.autoSize = TextFieldAutoSize.CENTER; var format:TextFormat=new TextFormat; //format.font="幼圆"; if (bold) { format.bold=true; } myTextField.defaultTextFormat=format; myTextField.text=Text; return myTextField; } /** * 创建一个button按钮 */ private static function getSureButton():Button{ var button:Button=new Button; button.name="sure"; button.label="确定"; button.width=40; button.height=20; button.textField.height=20; return button; } /** * 计算文本字符的长度 * @param str 待计算长度的文本字符 */ private static function CalLength(str:String):Number{ var charNum:uint=0; for(var i:uint=0;i<str.length;i++){ //大于255则是汉字,汉字占两个字符 if(str.charCodeAt(i)>255){ charNum+=2; } else{ charNum++; } } //返回width return charNum*6+5; } /** * 鼠标拖拽事件 */ private static function doStartDrag(event:MouseEvent):void { if(event.target.name=="holder"){ event.target.startDrag(); } } private static function doStopDrag(event:MouseEvent):void { if(event.target.name=="holder"){ event.target.stopDrag(); } } } }
参考:
http://download.csdn.net/detail/hfangyu/3699219
看下评论
转载于:https://blog.51cto.com/332374363/1257097