A client asked me to do something seemingly simple.
"I want the Alert to have just this one sentence bolded."
Well, it's not exactly simple, so here's how you do it:
there are two solutions as below :
import mx.core.IUITextField;
use namespace mx.core.mx_internal;
message +="Press Yes to continue or No to cancel";
}
if(message.length>0){
Alert.yesLabel="Yes"
var alert:Alert=Alert.show(message,"Alert",3,null,alertHandlerForSO);
// alert.mx_internal::alertForm.mx_internal::textField.htmlText = message;
IUITextField(alert.alertForm.textField).htmlText = message;
}
=========================
更多 Flex 4 示例,请到 http://www.slsay.com
Alert 控件是 Halo 控件中的一种。
所有的 Flex 组件都能够调用 Alert 类的静态方法 show ( ) ,来打开一个带有消息、标题、按钮和图表的弹出式对话框。
当点击了一个按钮时, Alert 控件将被关闭。
Alert.show ( ) 方法的语法如下:
public static show (
text:String,
title:String=null,
flags:uint=mx.controls.Alert.OK,
parent:Sprite=null,
clickListener:Function=null,
iconClass:Class=null,
defaultButton:uint=mx.controls.Alert.OK
):Alert
返回一个 Alert 控件对象。
下表描述了 show ( ) 方法的参数:
参数 | 描述 |
text | 对话框中的消息文本 |
title | 对话框标题栏中的文本 |
flags | 指定对话框按钮按钮 mx.controls.Alert.OK : Ok 按钮 mx.controls.Alert.YES : Yes 按钮 mx.controls.Alert.NO : No 按钮 mx.controls.Alert.CANCEL : Cancel 按钮 |
parent | Alert 控件的父对象 |
clickListener | 指定 click 事件的侦听器 |
iconClass | 指定对话框中消息文本左侧的图标 |
defaultButton | 使用一个标志参数的合法值指定默认按钮。当用户按下回车时,此按钮就被选中,其默认值是 Alert.OK. |
要使用 Alert 控件,需要引入 Alert 类,然后调用 show ( ) 方法,如下所示:
<?xml version="1.0"?>
<!-- http://www.slsay.com/archives/473 -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</mx:Script>
<mx:TextInput id="myInput"
width="150"
text=""/>
<mx:Button id="myButton"
label="Copy Text"
click="myText.text = myInput.text;
Alert.show('Text Copied!', 'Alert Box',
mx.controls.Alert.OK);"/>
<mx:TextInput id="myText"/>
</mx:Application>
此例中,当选择按钮就控件是,将复制 TextInput 控件中的文本至 TextArea 控件,并且显示 Alert 控件。
也可以定义 Button 控件的事件侦听器,如下所示:
<?xml version="1.0"?>
<!-- http://www.slsay.com/archives/473 -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function alertListener():void
{
myText.text = myInput.text;
Alert.show("Text Copied!", "Alert Box", Alert.OK);
}
]]>
</mx:Script>
<mx:TextInput id="myInput"
width="150"
text=""/>
<mx:Button id="myButton"
label="Copy Text"
click="alertListener();"/>
<mx:TextInput id="myText"/>
</mx:Application>
主题:在 show ( ) 方法创建对话框之后, Flex 仍在处理你的应用程序,而不是等待用户关闭对话框。
设置 Alert 控件的大小
Alert 控件将自行设置其大小以适应其文本、按钮和图标。可以使用 show ( ) 方法返回的 Alert 对象指定 Alert 控件的大小,如下所示:
<?xml version="1.0"?>
<!-- http://www.slsay.com/archives/473 -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
// Define variable to hold the Alert object.
public var myAlert:Alert;
private function openAlert():void
{
myAlert = Alert.show("Copy Text?", "Alert",
Alert.OK | Alert.CANCEL);
// Set the height and width of the Alert control.
myAlert.height=150;
myAlert.width=150;
}
]]>
</mx:Script>
<mx:TextInput id="myInput"
width="150"
text=""/>
<mx:Button id="myButton"
label="Copy Text"
click="openAlert();"/>
<mx:TextInput id="myText"/>
</mx:Application>
此例中通过设置 height 和width 属性来指定 Alert 对象的大小。
在 Alert 控件上使用时间侦听器
下面的例子为 Alert 控件的弹出式对话框添加了事件侦听器。事件侦听器在用户选择 Alert 控件中的按钮时执行处理过程。传递给事件侦听器的事件对象是 CloseEvent。
下面的例子中,只是在用户选择了OK 按钮时执行文本的复制。
<?xml version="1.0"?>
<!-- http://www.slsay.com/archives/473 -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
private function alertListener(eventObj:CloseEvent):void
{
// Check to see if the OK button was pressed.
if (eventObj.detail==Alert.OK) {
myText.text = myInput.text;
}
}
]]>
</mx:Script>
<mx:TextInput id="myInput"
width="150"
text="" />
<mx:Button id="myButton"
label="Copy Text"
click='Alert.show("Copy Text?", "Alert",
Alert.OK | Alert.CANCEL, this,
alertListener, null, Alert.OK);'/>
<mx:TextInput id="myText"/>
</mx:Application>
此例中,为 Alert 控件定义了一个事件侦听器。不用设置事件侦听器体,可以通过检查事件对象的 detail 属性来判定哪个按钮被按下。如果用户按了 OK 按钮就,则执行文本复制。如果用户按其他任何的按钮,或者按了 Esc 见,则不复制文本。
指定 Alert 控件的图标
可以在 Alert 控件中包含一个图标,显示在提示文本的左边。
<?xml version="1.0"?>
<!-- http://www.slsay.com/archives/473 -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
[Embed(source="assets/alertIcon.jpg")]
[Bindable]
public var iconSymbol:Class;
private function alertListener(eventObj:CloseEvent):void
{
// Check to see if the OK button was pressed.
if (eventObj.detail==Alert.OK) {
myText.text = myInput.text;
}
}
]]>
</mx:Script>
<mx:TextInput id="myInput"
width="150"
text=""/>
<mx:Button id="myButton"
label="Copy Text"
click='Alert.show("Copy Text?", "Alert",
Alert.OK | Alert.CANCEL, this,
alertListener, iconSymbol, Alert.OK );'/>
<mx:TextInput id="myText"/>
</mx:Application>