This example describes the simple use of the Android Introduction dialog box. Share to everyone for your reference, specific as follows:
Note: This article is just a learning note to record where you have learned
1. Get alertdialog static internal Class Builder object, this class to create a dialog box
2. Setting the caption button of the dialog box and the event that the button responds to through the builder object
3. Invoke the Create () method of the Builder dialog box
4. Call Alertdialog Show () method to display the dialog box
Main.xml file
<?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:id= "@+id/mytextview" android:layout_width= "
fill _parent "
android:layout_height=" wrap_content "
android:text=" @string/hello "
/>
<button
android:id= "@+id/mybutton"
android:layout_width= "fill_parent"
android:layout_height= "WRAP_" Content "
android:text=" Create alert dialog box
/>
</LinearLayout>
Mainactivity file
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mytextview = (TextView) Findviewbyid (R.id.mytextview);
MyButton = (Button) Findviewbyid (R.id.mybutton);
Add Alertdialog.builder object Final Alertdialog.builder Builder = new Alertdialog.builder (this); Adds a button event Mybutton.setonclicklistener (new View.onclicklistener () {@Override public void onclic for the button in the activity
K (View v) {builder.settitle ("Are you sure you want to delete this message?"). Set OK button Setpositivebutton ("Yes", new Onclicklistener () {@Override public void OnClick (Dialog
Interface dialog, int which) {mytextview.settext ("delete succeeded");
}
}). Set the Cancel button Setnegativebutton ("No", new Onclicklistener () {@Override public void OnClick (Dialogi
Nterface dialog, int which) {Mytextview.settext ("cancel Delete");
}
});
Create a dialog boxAlertdialog Alertdialog = Builder.create ();
Display dialog box Alertdialog.show ();
}
});
}
}
For more information on Android-related content readers can view the site topics: "The Android Control Usage Summary", "Android Resource Operation skills Summary", "Android File Operation skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android programming SD Card Operation method Summary", "Android Development introduction and Advanced Course", "Android programming activity Operation Skill Summary And the overview of Android View tips
I hope this article will help you with the Android program.