1 /**2 * Description:3 * <br/>site: <a href= "http://www.crazyit.org">crazyit.org</a>4 * <br/>copyright (C), 2001-2012, Yeeku.h.lee5 * <br/>this program was protected by copyright laws.6 * <br/>program Name:7 * <br/>date:8 * @authorYeeku.h.lee [email protected]9 * @version1.0Ten */ One Public classProgressdialogtestextendsActivity A { - //The program simulates populating an array with a length of 100 - Private int[] data =New int[100]; the intHasData = 0; - //define the identity of the progress dialog box - Final intProgress_dialog = 0x112; - //percent Complete of recording progress dialog box + intProgressstatus = 0; - ProgressDialog PD; + //define a handler that is responsible for the progress of the update A Handler Handler; at - @Override - Public voidonCreate (Bundle savedinstancestate) - { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); inButton EXECBN =(Button) Findviewbyid (r.id.exec); -Execbn.setonclicklistener (NewOnclicklistener () to { + Public voidOnClick (View source) - { the ShowDialog (progress_dialog); * } $ });Panax NotoginsengHandler =NewHandler () - { the @Override + Public voidhandlemessage (Message msg) A { the //indicates that the message was sent by the program. + if(Msg.what = = 0x111) - { $ pd.setprogress (progressstatus); $ } - } - }; the } - Wuyi @Override the PublicDialog Oncreatedialog (intID, Bundle status) - { WuSYSTEM.OUT.PRINTLN ("------Create------"); - Switch(ID) About { $ CaseProgress_dialog: - //Create Progress dialog box -PD =NewProgressDialog ( This); -Pd.setmax (100); A //setting the caption of a dialog box +Pd.settitle (% Task complete)); the //setting what the dialog box displays -Pd.setmessage ("percent complete for time-consuming tasks"); $ //The Settings dialog box cannot be closed with the Cancel button thePd.setcancelable (false); the //set the progress bar style of the dialog box the Pd.setprogressstyle (Progressdialog.style_spinner);//Round progress bar the //Pd.setprogressstyle (progressdialog.style_horizontal);//Horizontal progress bar - //sets whether the progress bar of the dialog box shows Progress inPd.setindeterminate (false); the Break; the } About returnPD; the } the the //The method will be recalled after the Oncreatedialog method call + @Override - Public voidOnpreparedialog (intID, Dialog Dialog) the {BayiSYSTEM.OUT.PRINTLN ("------Prepare------"); the Super. Onpreparedialog (ID, dialog); the Switch(ID) - { - CaseProgress_dialog: the //dialog box Progress clear 0 thePd.incrementprogressby (-pd.getprogress ()); the NewThread () the { - Public voidRun () the { the while(Progressstatus < 100) the {94 //get percent complete for time-consuming operations theProgressstatus =doWork (); the //send message to Handler theMessage m =NewMessage ();98M.what = 0x111; About //Send Message - Handler.sendmessage (m);101 }102 103 //if the task is completed104 if(Progressstatus >= 100) the {106 //Close the dialog box107 Pd.dismiss ();108 }109 } the }.start ();111 Break; the }113 } the the //simulates a time-consuming operation. the Public intdoWork ()117 {118 //Assigning a value to an array element119data[hasdata++] = (int) (Math.random () * 100); - Try121 {122Thread.Sleep (100);123 }124 Catch(interruptedexception e) the {126 e.printstacktrace ();127 } - returnHasData;129 } the}
Android ProgressDialog dialog box