Raising Toasts
A is a transient message, meaning that it displays and disappears on its own without user interaction. Moreover, it does not take focus away from the currently active , so if the user is busy writing the next Great American Programming Guide, they will not have keystrokes be “eaten” by the message.
Since a is transient, you have no way of knowing if the user even notices it. You get no acknowledgment from them, nor does the message stick around for a long time to pester the user. Hence, the Toast is mostly for advisory messages, such as indicating a long‑running background task is completed, the battery has dropped to a low‑but‑not‑too‑low level, etc.
Making a is fairly easy. The class offers a static that accepts a (or string resource ID) and returns a instance. The method also needs the (or other ) plus a duration. The duration is expressed in the form of the or constants to indicate, on a relative basis, how long the message should remain visible.
If you would prefer your be made out of some other , rather than be a boring old piece of text, simply create a new instance via the constructor (which takes a ), then call to supply it with the view to use and to set the duration.
Once your is configured, call its method, and the message will be displayed.
Alert! Alert!
If you would prefer something in the more classic dialog‑box style, what you want is an . As with any other modal dialog box, an 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 is to use the class. Following in true builder style, offers a series of methods to configure an , each method returning the for easy chaining. At the end, you call on the builder to display the dialog box.
Commonly used configuration methods on Builder include the following:
• if you want the “body” of the dialog to be a simple textual message, from either a supplied or a supplied string resource ID
• and to configure the text and/or icon to appear in the title bar of the dialog box
• , , and 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 beyond what the builder allows, instead of calling , call to get the partially built instance, configure it the rest of the way, then call one of the flavors of on the itself.
Once is called, the dialog box will appear and await user input.
Дата добавления: 2015-05-16; просмотров: 696;