Android dialog full explanation and sample code

Activities provides a dialog box mechanism for creating, saving and replying that is convenient for management, such as oncreatedialog (int), onprepareddialog (int, dialog), ShowDialog (int), disassdialog (int), etc. if these methods are used, the activity will return the dialog box managed by the activity through the getowneractivity () method

Oncreatedialog (int): when you use this callback function, the Android system will effectively set this activity as the owner of each dialog box, so as to automatically manage the state of each dialog box and attach it to the activity. In this way, each dialog box inherits the specific properties of the activity. For example, when a dialog box opens, the menu key displays the options menu defined for the activity, and the volume key modifies the audio stream used by the activity.

ShowDialog (int): when you want to display a dialog box, call the ShowDialog (int ID) method and pass an integer that uniquely identifies the dialog box. When the dialog box is requested for the first time, Android calls onCreateDialog (int ID) from your Activity, you should initialize this dialog box Dialog here. This callback method is passed with the same ID as ShowDialog (int ID). When you create this dialog box, return this object at the end of the activity.

Onpreparedialog (int, dialog): Android also calls the optional callback function onpreparedialog (int ID, dialog) before the dialog is displayed You can define this method if you want to change any properties of the dialog every time it is opened. This method is called every time the dialog box is opened, while oncreatedialog (int) is called only the first time the dialog box is opened. If you do not define onprepareddialog (), the dialog box will remain the same as when it was last opened. This method is also passed with the ID of the dialog box and the dialog object created in oncreatedialog().

Disassdialog (int): when you are ready to close the dialog, you can eliminate it by calling disassdialog () on the dialog. If necessary, you can also call the dismissDialog (int ID) method from this Activity, which will actually call the dismiss () method for this dialog box. If you want to use the oncreatedialog (int ID) method to manage the state of your dialog box (as discussed in the previous chapter), then each time your dialog box is eliminated, the state of the dialog object will be retained by the activity. If you decide that the object is no longer needed or it is important to clear the state, you should call removedialog (int ID). This will remove any internal object references, and if this dialog box is being displayed, it will be eliminated.

Here are the effects of several dialog boxes

Figure 1

Figure 2

Figure 3

Figure 4

Figure 5

Figure 6

Figure 7

Figure 1 Effect: this effect is to pop up a prompt when pressing the return button to ensure correct operation, using the common dialog box style.

Code:

Create dialog method dialog()

Figure 2 Effect: changed the chart of the dialog box and added three buttons

Figure 3 Effect: the information content is a simple view type

Figure 4 Effect: the information content is a group of radio boxes

Figure 5 Effect: the information content is a group of multiple selection boxes

Figure 6 Effect: the information content is a group of simple list items

Figure 7 Effect: the information content is a custom layout

1. Layout documents

2. Calling code

The above is the sorting of several implementation methods of Android dialog box. We will continue to supplement relevant materials in the future. Thank you for your support for this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>