Android offers four types of commonly used dialog boxes, and this article shares specific implementation methods:
- 1.AlertDialog, the most extensive function, the most widely used in practice
- 2.progressDialog, Progress Bar dialog box
- 3.DatePickerDialog, Date selection dialog box
- 4.TimePickerDialog, Time selection dialog box
Here the first, the remaining three are the first subclass, so its method, can be used directly.
Creating a dialog box typically requires a few of the following steps
- 1. Create a Alertdialog.builder object
- 2. Alertdialog.builder Settitle to set the title, SetIcon to set the icon
- 3. Call the Alertdialog.builder related method to set the content
- 4. Call Setpositivebutton or Setbegativebutton settings ok and Cancel button, which also has a decorative button, do not introduce the
- 5. Call the Alertdialog.builder create method for creation, and then call the Alertdialog Show method to display it.
Let's look at the specific usage based on the example
1. Simple Text dialog box
Define a button to add a click event for it to start the Alertdialog dialog box and add a listener event
public void Simpledialog1 (view view) {
Alertdialog.builder Builder = new Alertdialog.builder (this)
//Set dialog box title
. Settitle ("Simple Text dialog box")
//Set icon
. SetIcon (R.mipmap.ic_launcher)
//Set as Simple Text dialog box
. Setmessage (" This is a simple Text dialog box ");
Set two button
Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public
void OnClick (Dialoginterface dialog, int which) {
toast.maketext (mainactivity.this, "You clicked Confirm", Toast.length_short ). Show ();
}
};
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override public
void OnClick ( Dialoginterface dialog, int which) {
toast.maketext (mainactivity.this, "You clicked Cancel", Toast.length_short). Show ();
});
Builder.create (). Show ();
2. List Item dialog box
Unlike the Text dialog box above, the list looks like an array to display the list
The public void simpledialog2 (view view) {//list item needs to pass in an array to display the list final String items[] = {"Java", "PHP", "Android", "ios"}; Alertdialog.builder Builder = new Alertdialog.builder (this)//Set dialog title. Settitle ("Simple List Entry Dialog")//Set icon. SetI
Con (r.mipmap.ic_launcher)//Set as a Simple list item dialog box. Setitems (Items, new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "you clicked" + Items[which
], Toast.length_short. Show ();
}
}); Set two button Builder.setpositivebutton (OK, new Dialoginterface.onclicklistener () {@Override public void OnClick (Dia
Loginterface dialog, int which) {Toast.maketext (Mainactivity.this, "You clicked Confirm", Toast.length_short). Show ();
}
}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (DIALOGINTERFAC
e dialog, int which) {Toast.maketext (Mainactivity.this, "You clicked Cancel", Toast.length_short). Show (); }
});
Builder.create (). Show ();
}
3. Single Option dialog box
It's about the same as a list item, but it's a lot more powerful because incoming arguments can be arrays, cursor query result sets, and you can make ListAdapter
The public void Simpledialog3 (view view) {//list item needs to pass in an array to display the list final String items[] = {"Java", "PHP", "Android", "ios"}; Alertdialog.builder Builder = new Alertdialog.builder (this)//Set dialog title. Settitle ("Simple List Entry Dialog")//Set icon. SetI Con (r.mipmap.ic_launcher)//set to Simple Single List Items dialog box, 1 means the default selection of the second. Setsinglechoiceitems (items, 1, new Dialoginterface.onclick Listener () {@Override public void OnClick (Dialoginterface dialog, int which) {Toast.maketext (mainactivit
Y.this, "You clicked on" + Items[which], toast.length_short). Show ();
}
}); Set two button Builder.setpositivebutton (OK, new Dialoginterface.onclicklistener () {@Override public void OnClick (Dia
Loginterface dialog, int which) {Toast.maketext (Mainactivity.this, "You clicked Confirm", Toast.length_short). Show ();
}
}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Override public void OnClick (DIALOGINTERFAC e dialog, int which) {Toast.maketext (Mainactivity.this, "You clicked Cancel", toAst.
Length_short). Show ();
}
});
Builder.create (). Show ();
}
4, Multiple Options dialog box
Multiple-selection and radio-similar, there is a Boolean array to control the initial selection value
public void Simpledialog4 (view view) {
//list items need to pass in an array to display the list
final String items[] = {"Java", "PHP", "Android", "iOS "};
Alertdialog.builder Builder = new Alertdialog.builder (this)
//Set dialog title
. Settitle ("Simple list Item Dialog")
//Set icon
. SetIcon (R.mipmap.ic_launcher)
//set to the simple multiple-selection list Item dialog box, the Boolean variable represents the location of the initial selection
. Setmultichoiceitems (Items, new Boolean[]{false, False, False, true}, NULL);
Set two button
Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public
void OnClick (Dialoginterface dialog, int which) {
toast.maketext (mainactivity.this, "You clicked Confirm", Toast.length_short ). Show ();
}
};
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override public
void OnClick ( Dialoginterface dialog, int which) {
toast.maketext (mainactivity.this, "You clicked Cancel", Toast.length_short). Show ();
});
Builder.create (). Show ();
You can also use the Setadapter method to make a dialog box for a custom list item, or use Setview to make a custom View dialog box.
will be gradually applied in future instances, I hope you continue to pay attention.