Creating an alert dialog

How to

Create an alert dialog box in your application.

Solution

Construct the alert dialog box variable in the root of your class

var myAlert:AlertDialog = new AlertDialog();
// Placing this at the class root allows other 
//application elements to reference it.

Initialize the attributes of your dialog box

// Set title and body text.
myAlert.title = "Alert";
myAlert.message = "Do you accept this dialog message?";

// Add buttons.
myAlert.addButton("NO");
myAlert.addButton("YES");

// Add a listener for user clicking on a button.
myAlert.addEventListener(Event.SELECT, alertButtonClicked);

Add the call to display your dialog box where desired

// In this sample, it is fine to simply place this call 
// once right after setup to display one time on launch.
myAlert.show();

Create the function that is called by the button selection event of your dialog box

private function alertButtonClicked(event:Event):void{
	// Output index of selected button.
	trace("Button Clicked Index: " + event.target.selectedIndex);
}

Build Requirements

You must include the following classes in your project:

import qnx.fuse.ui.dialog.AlertDialog;
import flash.events.Event;

Discussion

When you create an alert dialog, you create your AlertDialog object, initialize its properties to whatever values your application requires, and finally call the show() function when you want to display the dialog. To add functionality, add an Event.SELECT listener function to perform any actions that are defined in the function when one of the buttons is selected. You can also reference which of your dialog's buttons was chosen by the user anywhere in the class by using the selectedIndex property.