Книга: Beginning Android

Alert! Alert!

Alert! Alert!

If you would prefer something in the more classic dialog-box style, what you want is an AlertDialog. As with any other modal dialog box, an AlertDialog pops up, grabs the focus, and stays there until closed by the user. You might use this for a critical error, a validation message that cannot be effectively displayed in the base activity UI, or something else where you are sure that the user needs to see the message and needs to see it now.

The simplest way to construct an AlertDialog is to use the Builder class. Following in true builder style, Builder offers a series of methods to configure an AlertDialog, each method returning the Builder for easy chaining. At the end, you call show() on the builder to display the dialog box.

Commonly used configuration methods on Builder include the following:

• setMessage() if you want the “body” of the dialog to be a simple textual message, from either a supplied String or a supplied string resource ID

• setTitle() and setIcon() to configure the text and/or icon to appear in the title bar of the dialog box

• setPositiveButton(), setNeutralButton(), and setNegativeButton() to indicate which button(s) should appear across the bottom of the dialog, where they should be positioned (left, center, or right, respectively), what their captions should be, and what logic should be invoked when the button is clicked (besides dismissing the dialog)

If you need to configure the AlertDialog beyond what the builder allows, instead of calling show(), call create() to get the partially built AlertDialog instance, configure it the rest of the way, then call one of the flavors of show() on the AlertDialog itself.

Once show() is called, the dialog box will appear and await user input.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.417. Запросов К БД/Cache: 2 / 0
Вверх Вниз