Dialog (pop-up box effect) is often used in Android, which is very powerful and can simulate n pop-up box effects. :
The following uses a small instance to demonstrate the powerful dialog in Android. The Code has been commented out in detail. You may try to manually comment on it.
Of course, due to the time relationship, there are still some functions not completed, some of the first display effects may also have bugs, and the Code also has a lot to be optimized. I hope you will be enthusiastic about this.
The following is the code (a little long ):
Package COM. chaoyang. activity; import Java. util. arraylist; import android. app. activity; import android. app. alertdialog; import android. app. progressdialog; import android. content. dialoginterface; import android. OS. bundle; import android. view. view; import android. widget. button; public class mainactivity extends activity {/** called when the activity is first created. */final string [] items = {"Zhang San", "Li Si", "Wang Wu", "Zhao 6 "," A cat "," a dog "}; int msinglechoiceid =-1; arraylist <integer> multichoiceid = new arraylist <integer> (); Private progressdialog mprogressdialog = NULL; private Static final int max_progress = 100; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main);/* Start sets the listener object for clicking those buttons */buttononcliklisten Listen = new buttononcliklisten (); B Utton btnconfim = (button) This. findviewbyid (R. id. btnconfim); button btncheck = (button) This. findviewbyid (R. id. btncheck); button btncustom = (button) This. findviewbyid (R. id. btncustom); button btnlist = (button) This. findviewbyid (R. id. btnlist); button btnmore = (button) This. findviewbyid (R. id. btnmore); button btnprogressbar = (button) This. findviewbyid (R. id. btnprogressbar); button btnread = (button) This. findvi Ewbyid (R. id. btnread); button btnridio = (button) This. findviewbyid (R. id. btnridio); btnconfim. setonclicklistener (Listen); btncheck. setonclicklistener (Listen); btnconfim. setonclicklistener (Listen); btnlist. setonclicklistener (Listen); btnmore. setonclicklistener (Listen); btnprogressbar. setonclicklistener (Listen); btnread. setonclicklistener (Listen); btnridio. setonclicklistener (Listen);/* end */} // define Public method. The message private void showdialogmsg (string MSG) {New alertdialog is displayed. builder (mainactivity. this ). setmessage (MSG ). show () ;}// defines an internal class, inherited to onclicklistener, specifically used to handle button-clicking events (Object-Oriented, flexible) private final class buttononcliklisten implements view. onclicklistener {@ overridepublic void onclick (view v) {// todo auto-generated method stubbutton button = (button) V; // receives the clicked Button Object alertdialog. builder = new alertdial Og. builder (mainactivity. this); // define a pop-up box object builder. seticon (R. drawable. aa); // set the iconswitch (v. GETID () {case R. id. btnconfim: builder. settitle ("are you sure you want to submit? "); // Set the title builder in the pop-up box. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {showdialogmsg ("You selected OK") ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {showdialogmsg ("you have chosen to cancel") ;}}); break; case R. id. btnmore: builder. settitle ("rating"); // Title builder. setmessage ("talk about Li Xiaoli's impression in your mind"); // pop-up message builder. setpositivebutton ("sexy", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub showdialogmsg ("you think she is very sexy ");}}); builder. setneutralbutton ("pretty", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub showdialogmsg ("you think she looks pretty ");}}); builder. setnegativebutton ("cool", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubshowdialogmsg ("you think she is cool") ;}}); break; case R. id. btnlist: builder. settitle ("list box"); builder. setitems (items, new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubshowdialogmsg ("you have selected the person whose ID is" + which +, name: "+ items [which]) ;}}); break; case R. id. btnridio: msinglechoiceid =-1; builder. settitle ("single answer"); builder. setsinglechoiceitems (items, 0, new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubmsinglechoiceid = which; showdialogmsg ("the id you selected is" + msinglechoiceid + ", name:" + items [which]) ;}}); builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubif (msinglechoiceid>-1) {showdialogmsg ("your final choice is" + items [msinglechoiceid]);} else {showdialogmsg ("your final choice is" + items [0]) ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub}); break; case R. id. btncheck: multichoiceid. clear (); // clear the set builder first. settitle ("Multiple Choices"); builder. setmultichoiceitems (items, new Boolean [] {false, false}, new dialoginterface. onmultichoiceclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which, Boolean ischecked) {// todo auto-generated method stubif (ischecked) {multichoiceid. add (which); showdialogmsg ("You selected" + items [which]);} else {multichoiceid. remove (which) ;}}); builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubstring STR = ""; for (INT I = 0; I <multichoiceid. size (); I ++) {STR + = items [multichoiceid. get (I)] + ",";} // remove the last "," If (Str. length ()> 0) {STR = Str. substring (0, str. length ()-1);} showdialogmsg ("you have selected a total of" + Str) ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub}); break; case R. id. btnprogressbar: mprogressdialog = new progressdialog (mainactivity. this); mprogressdialog. settitle ("progress bar window"); mprogressdialog. setprogressstyle (progressdialog. style_horizontal); // sets the style mprogressdialog. setmax (max_progress); // sets the maximum value mprogressdialog. setbutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// The logic after clicking is added here}); mprogressdialog. setbutton2 ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {// The logic after clicking is added here}); mprogressdialog. show (); New thread (). start (); return; case R. id. btncustom: break; case R. id. btnread: break;} builder. create (). show (); // create and display the pop-up box }}}
Layout file, relatively simple:
<? 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"/> <button Android: Id = "@ + ID/btnconfim" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "OK/cancel"/> <button Android: Id = "@ + ID/btnmore" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "multiple message prompts"/> <button Android: Id = "@ + ID/btnlist" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "list box"/> <button Android: Id = "@ + ID/btnprogressbar" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "progress bar"/> <button Android: Id = "@ + ID/btnridio" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "single screen"/> <button Android: Id = "@ + ID/btncheck" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Multi-choice box"/> <button Android: Id = "@ + ID/btncustom" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "custom layout"/> <button Android: Id = "@ + ID/btnread" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "read progress box"/> </linearlayout>