Building an Address Book > Creating the Application Functions > The TextField Focus Callback Functions

 

The TextField Focus Callback Functions

As we learned when we studied the init() function, on-screen text fields are represented by the ActionScript object TextField. Through the TextField object we can do everything from examine available fonts to replace currently selected text.

In our address book, we use the TextField object's new onSetFocus() and onKillFocus() handlers to highlight a text field when it is focused (that is, when the cursor is in it). In the init() function, we set callback functions for the onSetFocus() and onKillFocus() handlers. Now let's take a look at what those callbacks do.

The handleFieldFocus() callback performs the following task:

Sets the background color of the focused text field to light blue.

The handleFieldBlur() callback performs the following task:

Sets the background color of a blurred text field to dark blue.

The complete listings for handleFieldFocus() and handleFieldBlur() are shown below.

/*
 * Callback Function: handleFieldFocus()
 *   Desc: Highlights a text field when it receives focus.
 *   Parameters: None.
 */

function handleFieldFocus () 
{
  this.backgroundColor = 0x333399;
}


/*
 * Callback Function: handleFieldBlur()
 *   Desc: Removes highlight on a text field when it loses focus.
 *   Parameters: None.
 */

function handleFieldBlur () 
{
  this.backgroundColor = 0x322E5A;
}

Because the callback functions are invoked as methods of the TextField object, we can refer to the specific TextField that triggered the callback with the keyword this. So, within a TextField callback, we can set the backgroundColor property of the text field as follows:

  this.backgroundColor = 0x322E5A;  // Dark blue

Note that setting the background color of a TextField object doesn't automatically turn the background on. If the background is not already on, we must turn it on manually using the background property:

  // Turn on anyTextField's background.
  anyTextField.background = true;

In our addresss book, background enabling and disabling is handled by the functions enterEditMode() and exitEditMode().