Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 32

Programming actionscript 3.0
Table of Contents

Advertisement

The Greeter class now has a number of new features:
The
validNames
names when the Greeter class is loaded.
The
sayHello()
some conditions. If the
to prompt the user for a name. If the user name is valid, the greeting becomes
Finally, if either of those two conditions are not met, the
userName."
variable is set to
The
validName()
array, and
false
checks each of the strings in the
Array.indexOf()
in an array, or the value -1 if the object is not found in the array.
Next you will edit the Flash or Flex file that references this ActionScript class.
To modify the application using Flex Builder:
1.
Open the HelloWorld.mxml file.
2.
Next modify the
changing the background color to a light gray and preventing the user from editing the
displayed text:
<mx:TextArea id = "mainTxt" width="400" backgroundColor="#DDDDDD"
editable="false" />
3.
Now add the following lines right after the
a new TextInput field that lets the user enter a user name value:
<mx:HBox width="400">
<mx:Label text="User Name:"/>
<mx:TextInput id="userNameTxt" width="100%" enter="mainTxt.text =
myGreeter.sayHello(userNameTxt.text);" />
</mx:HBox>
The
attribute specifies that when the user presses the Enter key in the
enter
field, the text in the field will be passed to the
greeting displayed in the
The final contents of the HelloWorld.mxml file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
layout="vertical"
creationComplete = "initApp()" >
<mx:Script>
<![CDATA[
32
Getting Started with ActionScript
array lists valid user names. The array is initialized to a list of three
method now accepts a user name and changes the greeting based on
is an empty string (
userName
"Sorry, userName, you are not on the list."
method returns
if it is not found. The statement
validNames
method returns the index position of the first instance of an object
tag to indicate to the user that is is for display only, by
<mx:TextArea>
field will change accordingly.
mainTxt
), the
""
if the
true
inputName
validNames.indexOf(inputName)
array against the
<mx:TextArea>
Greeter.sayHello()
property is set
greeting
"Hello,
greeting
is found in the
validNames
string. The
inputName
closing tag. These lines create
userNameTxt
method, and the

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents