First, the common dialog box
Alertdialog: The most versatile and most widely used dialog box (the following three dialog boxes are subclasses of the dialog box)
ProgressDialog: Progress dialog box, this dialog box is just the packaging of the progress bar
Datepickerdialog: Date selection dialog, this dialog box is only for DatePicker packaging
Timepickerdialog: The Time Selection dialog box, which is the wrapper for Timepicker
Ii. Creating a dialog box with Alertdialog
Steps:
1, create Alertdialog.builder object;
2. Call Alertdialog.builder's Settitle () or Setcustomtitle () method to set the caption;
3, call Alertdialog.builder's SetIcon () method to set the icon;
4, call Alertdialog.builder related methods to set the content, Alertdialog specified the following 6 ways to specify the contents of the dialog box:
<1>setmessage () Setting dialog box content is simple text;
<2>setitems () Sets the contents of the dialog box to a simple list item;
<3>setsinglechoiceitems () Sets the content of the dialog box to be a single-selection list item;
<4>setmultichoiceitems () Sets the content of the dialog box to be a multi-select list item;
<5>setadapter () Sets the contents of the dialog box to a custom list item;
<6>setview () Settings dialog box content for custom view;
5, call Alertdialog.builder Setpositivebutton (), Setnegativebutton (), Setneutralbutton () method to add multiple buttons;
6. Call the Create () method of Alertdialog.builder. Alertdialog object, and then call the show () method of the Alertdialog object to display the dialog box.
eg.
public void simple (View source) {
Builder Builder=new Builder (this);
Setting the dialog box title
Builder.settitle ("Simple dialog box");
Set icon
Builder.seticon (R.drawable.ic_launcher);
Builder.setmessage ("The content of the test in the dialog box \ n the second line");
Add button
Builder.setpositivebutton ("OK", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Tv.settext ("Click OK" button);
}
});
Builder.setnegativebutton ("Cancel", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Tv.settext ("Click the Cancel button");
}
});
Builder.create ();
Builder.show ();
}
Execution Result:
Three, Simple List item dialog box
Call Alterdialog.builder's Setitems () method to set the Simple List Item dialog box, which needs to pass in an array or array resource ID when calling the method;
public void simple (View source) {
Builder Builder=new Builder (this);
Setting the dialog box title
Builder.settitle ("Simple List Item dialog box");
Set icon
Builder.seticon (R.drawable.ic_launcher);
Builder.setitems (items, new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Tv.settext ("You have selected" +items[which]+ ");
}
});
Add button
Builder.setpositivebutton ("OK", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Tv.settext ("Click OK" button);
}
});
Builder.setnegativebutton ("Cancel", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Tv.settext ("Click the Cancel button");
}
});
Builder.create ();
Builder.show ();
}
Iv. single-selection list item dialog box
android-dialog box