<1> Introduction
Alterdialog is a subclass dialog box that displays one, two, or three buttons. You can click the button displayed in the dialog box to trigger the corresponding event, and the prompt message you set is displayed in the dialog box.
If you only want to display a string in this dialog box, use the setmessage () method.
<2> procedure
1. Obtain the Buidler object of the static internal class alertdialog. This class creates the alertdialog object because the constructor of alertdialog is of the protected type.
2. Set the title, button, and event dialoginterface. onclicklistener of the dialog box through the Buidler object.
3. Call the CREATE () method creation dialog box of the Buidler.
4. Call the show () method of alterdialog to display the content.
<3> example
Package xiaosi. alertdialog; import android. app. activity; import android. app. alertdialog; import android. content. context; import android. content. dialoginterface; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class alertdialogactivity extends activity {private button = NULL;/** called when the activity is first create D. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. button); button. setonclicklistener (New buttonlistener ();} private class buttonlistener implements onclicklistener {public void onclick (view v) {showdialog (alertdialogactivity. this) ;}} private void showdialog (context) {ALER Tdialog. builder = new alertdialog. builder (context); builder. seticon (R. drawable. a); builder. settitle ("prompt"); builder. setmessage ("are you sure you want to play? "); Builder. setpositivebutton ("yes", new android. content. dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {// todo auto-generated method stub settitle ("yes") ;}}); builder. setneutralbutton ("no", new android. content. dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {settitle ("no") ;}}); builder. setnegativebutton ("cancel", new android. content. dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int which) {settitle ("cancel") ;}}); builder. show ();}}
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <button Android: id = "@ + ID/button" Android: layout_height = "wrap_content" Android: layout_width = "fill_parent" Android: text = "click"/> </linearlayout>