Res/layout/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 "> <Textview Android: layout_width =" Fill_parent "Android: layout_height =" Wrap_content "Android: text =" @ String/Hello "Android: Id =" @ + ID/tvshowtext "/> <Button Android: layout_width =" Wrap_content "Android: layout_height =" Wrap_content "Android: Id =" @ + ID/btnshowalert "Android: text =" @ String/btn_name "> </Button> </linearlayout>
Res/values/strings. xml
<? XML version ="1.0"Encoding ="UTF-8"?> <Resources> <string name ="Hello"> Hello world, ex03_11! </String> <string name ="App_name"> Ex03_11 </string> <string name ="Btn_name"> Click" I "to bring up the dialog box </string> <string name ="Alert_title"> About </string> <string name ="Alert_msg"> Hello Android !!! </String> </resources>
Src/ex03_11.java
Package gphone. ex03_11; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. textview; public class ex03_11 extends activity {button btnshowalert = NULL; textview tvshowtext = NULL;/** called when the activity is first created. * // @ override public void oncreate (Bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); tvshowtext = (textview) ex03_11.this.findviewbyid (R. id. tvshowtext); btnshowalert = (button) ex03_11.this.findviewbyid (R. id. btnshowalert); btnshowalert. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stub/* new android. app. alertdialog. builder (Ex03_11.this) */New alertdialog. builder (ex03_11.this ). settitle (R. string. alert_title ). setmessage (R. string. alert_msg ). setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub // ex03_11.this.finish (); tvshowtext. settext ("Hello Android !!! ") ;}}). Show ();}});}}
Running result note: Because I am using a miui-customized system, the displayed alertdialog is different. To download the mini Rom, click www.miui.com.