
Associate the listener with an event with MXML 147
The keyword public specifies the function’s scope. In this case, the function is available
throughout your code.
The keyword
void specifies the function’s return type. All ActionScript functions should
specify a return type. The
convertCurrency function returns no value.
The price entered by the user,
txtPrice.text, is cast as a number and then validated to
make sure the user entered a number. If the price is a number, the calculation is performed
and the result is cast back to a string for display in the lblResults control.
In a real-world application, the value of the rate variable would be set at run time by
calling a web service and retrieving the current exchange rate. As well, the result would be
formatted as a currency.
4. Save the file.
Associate the listener with an event with
MXML
Associating a listener to an event, or registering it, means to include logic in your application
that notifies the listener of a triggering event of a particular type from a particular source. For
your application, you want the event listener to be notified of a click event from the Convert
to Yen button. When notified that the event has occurred, the listener performs the currency
calculation and displays the results.
One way to register the listener is to specify it as the value of the
click property in the
<mx:Button> tag.
You can also use ActionScript to register the listener. For more information, see “Associate the
listener with an event with ActionScript” on page 148.
1. In Design mode, select the Button control and enter convertCurrency() in the On Click
text box in the Properties view.
Comentários a estes Manuais