String.xml
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <string name= "App_name" >TestBase< /string> <string name= "City_name" > City </string> <string name= "City_default" > Beijing </ string> <string name= "Btn_yesorno" > whether to select the Prompt box </string> <string name= "Btn_listitem" > List selection Prompt box </string> </resources>
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= "com.mytest.testbase.MainActivity" > <textview android:la Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/city_name"/> <edittext android:id= "@+id/city_name" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" android:hint= "@string/city_default" android:inputtype= "text"/> <bu Tton android:id= "@+id/btn_yesorno" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:onclick=" Yesornoonclick "android:text=" @string/btn_yesorno "/> <bu Tton android:id= "@+id/btn_listitem" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:onclick= "ListI Temonclick "android:text=" @string/btn_listitem "/> <textview android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:id=" @+id/tv_result "/> </LinearLayout>
Activity.java
Package com.mytest.testbase; Import android.app.Activity; Import Android.app.AlertDialog; Import Android.app.AlertDialog.Builder; Import Android.content.DialogInterface; Import Android.os.Bundle; Import Android.view.View; Import Android.widget.TextView; Import Android.widget.Toast; public class Mainactivity extends Activity {private TextView textview_result; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Textview_result = (TextView) This.findviewbyid (R.id.tv_result); /** * Select the Prompt box * * @param view */public void Yesornoonclick (view view) {Bui Lder builder = new Alertdialog.builder (this); Builder.setmessage ("Confirm the deletion?" "). Setpositivebutton (" Yes ", new Dialoginterface.onclicklistener () {@Override public void onclic K (dialoginterface Dialog, int which) { Toast.maketext (Getapplicationcontext (), "Delete succeeded",//Toast.length_short). Show (); Textview_result.settext ("delete succeeded"); }}). Setnegativebutton ("No", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {//Toast.maketext (Getapplicationcontext (), "Delete failed", Toast.length_short). Show (); Textview_result.settext ("delete failed"); } }); Alertdialog dialog = Builder.create (); Dialog.show (); /** * List item selection Prompt box * * @param view */public void Listitemonclick (view view) {Bui Lder builder = new Alertdialog.builder (this); Final string[] Cities = {"Guangzhou", "Shanghai", "Beijing", "Hong Kong", "Macau"}; Builder.settitle ("Select City"). Setitems (Cities, New Dialoginterface.onclicklistener () {@Override PU Blic void ONclick (dialoginterface dialog, int which) {textview_result.settext ("Selected city is" + Cities[which]); } }); Alertdialog dialog = Builder.create (); Dialog.show (); } }
Android Basics: Cue box Alertdialog instance