Display the dialog box window
1. Create the DIALOG1 project and add a button to the Activity_main.xml file:
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <button android:id=" @+id/btn_dialog " android:layout_width=" Fill_parent " android:layout_height= "wrap_content" android:onclick= "OnClick" android:text= "click to display a Dialog "/></relativelayout>
2. Implement the Create dialog in the Mainactivity.java file and add the following code:
Package Com.example.dialog;import Android.os.bundle;import Android.app.activity;import android.app.AlertDialog; Import Android.app.dialog;import android.content.dialoginterface;import Android.view.menu;import Android.view.View Import Android.widget.toast;public class Mainactivity extends Activity {charsequence[] items = {"Google", "Apple", "MICR Osoft "};boolean[] itemschecked = new Boolean[items.length]; @Overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @SuppressWarnings ("deprecation") public void OnClick (View v) {showDialog (0);//When the button is clicked, the dialog box is displayed, and this method takes an integer parameter, Used to identify the specific dialog box to be displayed (only the dialog box labeled 0 is created). The @Override @deprecated//oncreatedialog () method is a callback method that is used to create a dialog box that is managed by the activity. This callback method is called when ShowDialog () is called. protected Dialog oncreatedialog (int id) {//TODO auto-generated method Stubswitch (ID) {Case 0://to create a dialog box, you need to use the Alertdialog class's builder constructor to set different properties, labels, headings, buttons, and check boxes. return new Alertdialog.builder (this)//Call the Alertdialog object's Builder constructor: SetIcon (R.drawAble.ic_launcher). Settitle ("This was a dialog with some simple text ..."). Setpositivebutton ("OK",//Set OK button new Dialoginterface.onclicklistener () {//Set click event @overridepublic void OnClick (Dialoginterface dialog,int which) {//TODO Auto-generated Method Stubtoast.maketext (Getbasecontext (), "OK clicked!", Toast.length_short). Show ();}}). Setnegativebutton ("Cancel",//Set Cancel button New Dialoginterface.onclicklistener () {//Set click event @overridepublic void OnClick (Dialoginterface dialog,int which) {//TODO auto-generated method Stubtoast.maketext (Getbasecontext (), "Cancel clicked!", Toast.length_short). Show ();}}). Setmultichoiceitems (items, itemschecked,//set check box new Dialoginterface.onmultichoiceclicklistener () {// Set Click event @overridepublic void OnClick (Dialoginterface dialog,int which, Boolean isChecked) {//TODO auto-generated Method St Ubtoast.maketext (Getbasecontext (), items[which]//selects the returned message by passing in the parameter ischecked to verify whether it is clicked. + (isChecked?) "checked!": "unchecked!"), Toast.length_short). Show ();}}). Create ();} return null;} @OverridepublIC boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is Present.getmenu Inflater (). Inflate (R.menu.main, menu); return true;}}
3, run a bit, display such as:
Click the button: