Home>

Steps to create alertdialog:

1, create alertdialog.builder object

2, call the settitle method of the builder object to set the title,seticon method set icon

3, call builder related methods such as setmessage method, setitems method, setsinglechoiceitems method, setmultichoiceitems method, setadapter method, setview method to set different types of dialog box content.

4, call setpositivebutton, setnegativebutton, setneutralbutton to set multiple buttons

5. Call the create () method of the builder object to create an alertdialog object

6. Call the show () method of the alertdialog object to display the dialog box

First, setmessage:set the content of the dialog box to simple text content

//Create a builder
alertdialog.builder builder=new alertdialog.builder (this);
//Setting parameters
builder.settitle ("Please make a selection"). seticon (r.drawable.ic_launcher)
.setmessage ("Sister Feng is beautiful")
.setpositivebutton ("beauty", new onclicklistener () {//positive
@override
public void onclick (dialoginterface dialog,int which) {
//todo auto-generated method stub
toast.maketext (mainactivity.this, "Congratulations you made a mistake", 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, you got it right", 0)
.show ();
}
}). setneutralbutton ("I don't know", new onclicklistener () {//middle level
@override
public void onclick (dialoginterface dialog,int which) {
//todo auto-generated method stub
toast.maketext (mainactivity.this, "You blind", 0)
.show ();
}
});
builder.create (). show ();

Run screenshot:

Second, setitem:set the text box content to 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);
//Setting parameters
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 screenshot:

Third, setsinglechoiceitems () sets the dialog box content to 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);
//Setting parameters
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 screenshot:

Fourth, setmultichoiceitems () sets the dialog box content to a multi-option list

//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 screenshot:

Five, setadapter () sets the dialog box content to 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 screenshot:

Six, setview () sets the dialog box to a custom view

//Create dialog builder
alertdialog.builder builder=new alertdialog.builder (this);
//Get layout
view view2=view.inflate (mainactivity.this, r.layout.login, null);
//Get the controls 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);
//Setting parameters
builder.settitle ("login"). seticon (r.drawable.ic_launcher)
.setview (view2);
//create dialog
final alertdialog alertdialog=builder.create ();
button.setonclicklistener (new view.onclicklistener () {
@override
public void onclick (view v) {
//todo auto-generated method 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 ();//The dialog disappears
}
});
alertdialog.show ();

Run screenshot:

  • Previous Detailed usage of telldir () function and seekdir () function in C language
  • Next Background image of the infinite loop of Android combat aircraft game (2)