In addition to the "dialog box" progress bar, you can also create a dialog box that displays the "Operation progress", for example, displaying the download status.
1. Create a project: dialog.
2. Code in Main. xml.
[Java]View plaincopy
- <? 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: Orientation = "vertical">
- <Button
- Android: Id = "@ + ID/btn_dialog3"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: onclick = "onclick3"
- Android: text = "click to display a detailed progress dialog"/>
- </Linearlayout>
3. Code in dialogactivity. java.[Java]View plaincopy
- Public class dialogactivity extends activity {
- Progressdialog;
- /** Called when the activity is first created .*/
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- }
- Public void onclick3 (view v ){
- Showdialog (1 );
- Progressdialog. setprogress (0 );
- New thread (New runnable (){
- Public void run (){
- For (INT I = 1; I <= 15; I ++ ){
- Try {
- // --- Simulate doing something lengthy ---
- Thread. Sleep (1000 );
- // --- Update the dialog ---
- Progressdialog. incrementprogressby (INT) (100/15 ));
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- }
- Progressdialog. Dismiss ();
- }
- }). Start ();
- }
- @ Override
- Protected dialog oncreatedialog (int id ){
- Switch (ID ){
- Case 1:
- Progressdialog = new progressdialog (this );
- Progressdialog. seticon (R. drawable. ic_launcher );
- Progressdialog. settitle ("downloading files ...");
- Progressdialog. setprogressstyle (progressdialog. style_horizontal );
- Progressdialog. setbutton (dialoginterface. button_positive, "OK ",
- New dialoginterface. onclicklistener (){
- Public void onclick (dialoginterface dialog,
- Int whichbutton ){
- Toast. maketext (getbasecontext (), "OK clicked! ",
- Toast. length_short). Show ();
- }
- });
- Progressdialog. setbutton (dialoginterface. button_negative, "cancel ",
- New dialoginterface. onclicklistener (){
- Public void onclick (dialoginterface dialog,
- Int whichbutton ){
- Toast. maketext (getbasecontext (), "cancel clicked! ",
- Toast. length_short). Show ();
- }
- });
- Return progressdialog;
- }
- Return NULL;
- }
- }
4. debug by F11.
To create a dialog box that shows the progress, you must first create an instance of the progressdialog class, and then set various statuses, icons, titles, styles, and so on:
[Java]View plaincopy
- Progressdialog = new progressdialog (this );
- Progressdialog. seticon (R. drawable. ic_launcher );
- Progressdialog. settitle ("downloading files ...");
- Progressdialog. setprogressstyle (progressdialog. style_horizontal );
Then, set two buttons:[Java]View plaincopy
- Progressdialog. setbutton (dialoginterface. button_positive, "OK ",
- New dialoginterface. onclicklistener (){
- Public void onclick (dialoginterface dialog,
- Int whichbutton ){
- Toast. maketext (getbasecontext (), "OK clicked! ",
- Toast. length_short). Show ();
- }
- });
- Progressdialog. setbutton (dialoginterface. button_negative, "cancel ",
- New dialoginterface. onclicklistener (){
- Public void onclick (dialoginterface dialog,
- Int whichbutton ){
- Toast. maketext (getbasecontext (), "cancel clicked! ",
- Toast. length_short). Show ();
- }
- });
Use a thread to display the progress bar status:[Java]View plaincopy
- Progressdialog. setprogress (0 );
- New thread (New runnable (){
- Public void run (){
- For (INT I = 1; I <= 15; I ++ ){
- Try {
- // --- Simulate doing something lengthy ---
- Thread. Sleep (1000 );
- // --- Update the dialog ---
- Progressdialog. incrementprogressby (INT) (100/15 ));
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- }
- Progressdialog. Dismiss ();
- }
- }). Start ();
When the progress bar reaches 100%, it is removed.