Six ways to create Alertdialog in Android _android

Source: Internet
Author: User
Tags gettext stub

To create a alertdialog:

1. Create Alertdialog.builder objects

2, call the Builder object Settitle method set the caption, SetIcon method set icon

3, call Builder related methods such as Setmessage method, Setitems method, Setsinglechoiceitems method, Setmultichoiceitems method, Setadapter method, The Setview method sets different types of dialog box contents.

4, call Setpositivebutton, Setnegativebutton, Setneutralbutton set multiple buttons

5. Create Alertdialog object by calling the Create () method of the Builder object

6, call the Alertdialog object's show () method to display the dialog box

First, Setmessage: Set dialog box content for simple text content

Create Builder
Alertdialog.builder Builder = new Alertdialog.builder (this);
Set
the parameter Builder.settitle (please make a selection). SetIcon (R.drawable.ic_launcher).
setmessage ("Feng elder sister Beauty").
Setpositivebutton ("Beauty", new Onclicklistener () {//Active
@Override public
void OnClick (Dialoginterface dialog,
int which) {
//TODO auto-generated method Stub
toast.maketext (Mainactivity.this, "Congratulations on the wrong call", 0)
. Show ():
}
. Setnegativebutton ("Not Beautiful", new Onclicklistener () {//negative
@Override public
void OnClick (Dialoginterface dialog,
int which) {
//TODO auto-generated method Stub
toast.maketext (Mainactivity.this, "Congratulations", 0)
. Show ();
. Setneutralbutton ("Unknown", new Onclicklistener () {//Intermediate
@Override public
void OnClick (dialoginterface dialog,
int which) {
//TODO auto-generated method Stub
toast.maketext (Mainactivity.this, "You're Blind", 0)
. Show ();
}
});
Builder.create (). Show ();

Run Screenshots:

Second, SetItem: Set text box content as a simple list item

Create data
final string[] items = new string[] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"};
Create dialog Builder
alertdialog.builder Builder = new Alertdialog.builder (this);
Set the parameter
Builder.seticon (r.drawable.ic_launcher). Settitle ("hint")
. Setitems (items, new Onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
Toast.maketext (mainactivity.this, Items[which],
toast.length_short). Show ();
}
};
Builder.create (). Show ();

Run Screenshots:

Three, Setsinglechoiceitems () Settings dialog box content is a radio list item

Create data
final string[] items = new string[] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"};
Create dialog Builder
alertdialog.builder Builder = new Alertdialog.builder (this);
Set the parameter
Builder.seticon (r.drawable.ic_launcher). Settitle ("hint")
. Setsinglechoiceitems (items, 0, new Onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
//TODO auto-generated method Stub
toast.maketext (Mainactivity.this, Items[which),
toast.length_short). Show ();
});
Builder.create (). Show ();

Run Screenshots:

Four, Setmultichoiceitems () Settings dialog box content is a list of multiple options

Create data
final string[] items = new string[] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"};
Create dialog Builder
alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (R.drawable.ic_launcher)
. Settitle ("hint")
. Setmultichoiceitems (items,
new boolean[] { True, True, False, False, false},
new Onmultichoiceclicklistener () {
@Override public
void OnClick ( Dialoginterface dialog,
int which, Boolean ischecked) {
//TODO auto-generated method Stub
if (ischecked) {
Toast.maketext (mainactivity.this,
Items[which], 0). Show ();}}
;
Builder.create (). Show ();

Run Screenshots:

Five, Setadapter () Set dialog box content is a custom list item (here is a layout)

Create data
final string[] items = new string[] {"Beijing", "Shanghai", "Guangzhou", "Shenzhen"};
Create dialog Builder
alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.settitle ("hint")
. SetIcon (R.drawable.ic_launcher)
. Setadapter (
new arrayadapter<string> (Mainactivity.this,
R.layout.item, r.id.tv, items),
new Onclicklistener () {
@Override public
void OnClick (dialoginterface dialog,
int which) {
//TODO auto-generated method Stub
toast.maketext (Mainactivity.this, Items[which] ,
0). Show ();
}
};
Builder.create (). Show ();

Run Screenshots:

VI. Setview () Setup dialog box for custom view

 Create Dialog builder Alertdialog.builder Builder = new Alertdialog.builder (this);
Get layout View view2 = View.inflate (mainactivity.this, r.layout.login, NULL);
Gets the control in the layout final edittext username = (edittext) View2.findviewbyid (r.id.username);
Final edittext password = (edittext) View2.findviewbyid (R.id.password);
Final button button = (button) View2.findviewbyid (R.id.btn_login);
Sets the parameter Builder.settitle ("Login"). SetIcon (R.drawable.ic_launcher). Setview (VIEW2);
Create a dialog box final alertdialog alertdialog = Builder.create (); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Meth
OD stub String uname = Username.gettext (). toString (). Trim ();
String psd = Password.gettext (). toString (). Trim ();
if (Uname.equals ("ZCL") && psd.equals ("123456")) {Toast.maketext (mainactivity.this, "login succeeded", 0). Show ();
Toast.maketext (mainactivity.this, "Login Failed", 0). Show ();
Alertdialog.dismiss ();//dialog box disappears}}); Alertdialog.show ();

Run Screenshots:

 

The above is a small set to introduce the Android Alertdialog six ways to create, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.