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.
<? 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. Package net. horsttnann. Dialog;
Import android. app. Activity;
Import android. app. Dialog;
Import android. app. ProgressDialog;
Import android. content. DialogInterface;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Toast;
Public class DialogActivity extends Activity {
ProgressDialog 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 ();
}
Www.2cto.com
@ 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. Perform F11 debugging.
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:
ProgressDialog = new ProgressDialog (this );
ProgressDialog. setIcon (R. drawable. ic_launcher );
ProgressDialog. setTitle ("Downloading files ...");
ProgressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL );
Then set two buttons: 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: 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.
Written by manoel