Android: Daily Learning Notes (8) ——— Explore UI Development (2) dialog box
Description:
A dialog box is a small window that prompts the user to make a decision or enter additional information. The dialog box does not populate the screen and is typically used for modal events that require the user to take action to continue execution.
Tips:
DialogA class is the base class for a dialog box, but you should avoid direct instantiation Dialog and use one of the following subclasses:
-
AlertDialogThis dialog box displays a title, up to three buttons, a list of selectable items, or a custom layout.
DatePickerDialogor TimePickerDialog This dialog box has a predefined UI that allows the user to select a date or time.
These classes define the style and structure of your dialog box, but you should use it DialogFragment as a container for dialog boxes .
The DialogFragment class provides all the controls you need to create a dialog box and manage its appearance, rather than calling Dialog methods on the object .
Description
Use the DialogFragment Administration dialog box to ensure that it correctly handles life cycle events, such as when a user presses the back button or rotates the screen. In addition, the DialogFragment class allows you to reuse the UI of a dialog box as an embedded component in a larger UI, just as you would in a tradition Fragment (for example, when you want the dialog UI to have a different look and feel on a large screen and a small screen).
a simple way to create a dialog fragment:
Button dialog =(Button) Findviewbyid (R.id.button_dialog); Dialog.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {Alertdialog.builder dialog=NewAlertdialog.builder (mainactivity. This); Dialog.settitle ( "dialog box title"); Dialog.setmessage ("dialog box to display information"); Dialog.setcancelable (false); Dialog.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich) { } }); Dialog.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich) { } }); Dialog.show (); } });
Android: Daily Learning Notes (8) ——— Explore UI Development (2)