A few common methods:
1.setTitle ()
Sets the caption displayed by the dialog box.
2.setIcon ()
Setting the icon for the dialog box, it is worth noting that SetIcon () will not take effect if the Settitle () method is not used.
3.setMessage ()
The Settings dialog box displays the contents.
4.setPositiveButton (), Setnegativebutton (), Setneutralbutton ()
Sets the button for the dialog box.
5.setCancelable ()
Clicking the dialog box does not disappear, and pressing the Back Button dialog box will not disappear.
Prompt dialog box
Demo Example:
Mainactivity.java
public class Mainactivity extends Activity {button button; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View v) {alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); builder.setmessage ("This is a cue box"); Builder.seticon (R.drawable.icon); Builder.settitle ("hint"); Builder.setpositivebutton ("Positive", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int arg1) {}}); Builder.setnegativebutton ("negative", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int ARG1) {//TODO auto-generated Method stub}}); Builder.setneutralbutton ("Neutral", new Onclicklistener () {@ overridepublic void OnClick (dialoginterface arg0, int arg1) {//TODO auto-generated Method stub}}); Builder.show ();}}
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <button android:id= "@+id/but" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "tap" android:onclick= "click"/></linearlayout>
List dialog Box
Demo Example:
Mainactivity.java
public class Mainactivity extends Activity {button button; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View v) {alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this);// Builder.setmessage ("I am a hint dialog"); After setting setmessage, Setitems fails Builder.seticon (R.drawable.icon); Builder.settitle ("hint"); Builder.setitems (new String[] {" One "," "" "," "Three"},new Onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int index) {String str = Null;switch (index) {case 0:STR = "One", break;case 1:str = "one"; break;case 2:str = "three"; break;} Toast.maketext (Mainactivity.this, str, toast.length_short). Show ();}}); Builder.show ();}}
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <button android:id= "@+id/but" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "tap" android:onclick= "click"/></linearlayout>
Single-selection list dialog box
Demo Example:
Mainactivity.java
public class Mainactivity extends Activity {button button; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View v) {alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.seticon ( R.drawable.icon); Builder.settitle ("hint"); Builder.setsinglechoiceitems (new string[] {"One", "one", "three"}, 0,new Onclicklistener () {@Overridepublic void OnClick (dialoginterface arg0, int index) {String str = null;switch (index) {case 0 : str = "one"; break;case 1:str = "one"; break;case 2:str = "three"; break;} Toast.maketext (Mainactivity.this, Str,toast.length_short). Show ();}}); Builder.setpositivebutton ("confirm", null); Builder.show ();}}
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <button android:id= "@+id/but" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "tap" android:onclick= "click"/></linearlayout>
Check List dialog box
Demo Example:
Mainactivity.java
public class Mainactivity extends Activity {button button; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View v) {alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.seticon ( R.drawable.icon); Builder.settitle ("hint"); final boolean[] items = new boolean[] {false, False, false}; Builder.setmultichoiceitems (new string[] {"One", "one", "three"},items, new Onmultichoiceclicklistener () {@ overridepublic void OnClick (dialoginterface arg0, int arg1,boolean ischeaked) {}}); Builder.setpositivebutton ("Confirm", NULL); Builder.show ();}}
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <button android:id= "@+id/but" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "tap" android:onclick= "click"/></linearlayout>
Customize dialog box
Mainactivity.java
public class Mainactivity extends Activity {button button; @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View v) {alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.settitle (" Hint "); Builder.seticon (R.drawable.icon); ImageView ImageView = new ImageView (mainactivity.this); Imageview.setimageresource (r.drawable.close);//Get layout file View MyView = This.getlayoutinflater (). Inflate (R.layout.form , null); Builder.setview (MyView); ListView ListView = (ListView) Myview.findviewbyid (r.id.lv); arrayadapter<string> adapter = new Arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_1,new string[] {"1", "2", "3"}); Listview.setadapter (adapter); Listview.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1,int positon, long Arg3) {Toast.maketext (Mainactivity.this, "" + Positon, Toast.lenGth_short). Show ();}}); Builder.setpositivebutton ("confirm", null); Builder.show ();}}
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <button android:id= "@+id/but" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "tap" android:onclick= "click"/> </LinearLayout>
Form.xml (The layout content displayed by the dialog box)
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <listview android:id= "@+id/lv" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" ></ListView> </LinearLayout>
The Alertdialog of Android development