Sometimes, you may need to pop up a dialog box to get some confirmation from the user's input. In this case, you can override the protected method (protected) Oncreatedialog () in the activity base class.
1. Create a project: Dialog.
2. Code in the Main.xml.
<?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:o" rientation= "vertical" >
<button
android:id= "@+id/btn_dialog" android:layout_width= "Fill_"
Parent "
android:layout_height=" wrap_content "android:text=" click to
Display a dialog "
Android:onclick = "OnClick"/>
</LinearLayout>
3. Code in the Dialogactivity.java.
public class Dialogactivity extends activity {charsequence[] items = {"Google", "Apple", "Microsoft"};
boolean[] itemschecked = new Boolean[items.length]; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
public void OnClick (View v) {showdialog (0); @Override protected Dialog oncreatedialog (int id) {switch (ID) {case
0:return New Alertdialog.builder (this). SetIcon (R.drawable.ic_launcher) . Settitle ("This are a dialog with some simple text ..."). Setpositivebutton ("OK" , new Dialoginterface.onclicklistener () {public voi D onClick (dialoginterface dialog, int whichbutton) { Toast.maketext (Getbasecontext (), "OK clicked!", toast.length
_short). Show ();
}). Setnegativebutton ("Cancel", New Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog , int whichbutton) {Toast.maketext (
Getbasecontext (), "Cancel clicked!",
Toast.length_short). Show (); }). Setmultichoiceitems (iteMS, itemschecked, new Dialoginterface.onmultichoiceclicklistener () { public void OnClick (Dialoginterface dialog, int which, Boole
An ischecked) {Toast.maketext (
Getbasecontext (), Items[which] + (ischecked?)
"Checked!"
: "unchecked!"), Toast.length_short). Show ();
). Create ();
return null; }
}