Mainactivity is as follows:
Package CN. testalertdialog; import android. app. activity; import android. app. alertdialog. builder; import android. app. dialog; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. view. view. onclicklistener; import android. widget. button;/*** custom dialog usage */public class mainactivity extends activity {private button; private dialog mdialog; @ overrideprote CTED void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {button = (button) findviewbyid (R. id. button); button. setonclicklistener (New buttononclicklistenerimpl ();} private class buttononclicklistenerimpl implements onclicklistener {@ overridepublic void onclick (view) {Switch (view. GETID () {case R. id. button: layou Tinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service); view dialogview = Inflater. inflate (R. layout. dialogview, null); Builder = new Builder (mainactivity. this); builder. setview (dialogview); mdialog = builder. create (); mdialog. show (); initdialogccomponents (dialogview); break; default: Break ;}}// initialize the private void initdialogccomponents (view dialogview) {button firstb Utton = (button) dialogview. findviewbyid (R. id. dialog_firstbutton); firstbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {system. out. println ("click the first button") ;}}); button secondbutton = (button) dialogview. findviewbyid (R. id. dialog_secondbutton); secondbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {If (mdialog! = NULL & mdialog. isshowing () {mdialog. Dismiss ();}}});}}
Main. XML is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_tip" android:textSize="20sp" android:layout_centerInParent="true" /></RelativeLayout>
Dialogview. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <button Android: id = "@ + ID/dialog_firstbutton" Android: layout_width = "150dip" Android: layout_height = "50dip" Android: text = "first button" Android: layout_gravity = "center_horizontal"/> <button Android: Id = "@ + ID/dialog_secondbutton" Android: layout_width = "150dip" Android: layout_height = "50dip" Android: TEXT = "the first button" Android: layout_gravity = "center_horizontal"/> </linearlayout>