Android Program Development: (1) detailed activity-1.6 display complex dialog box

Source: Internet
Author: User
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
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: Orientation = "vertical">
  6. <Button
  7. Android: Id = "@ + ID/btn_dialog3"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"
  10. Android: onclick = "onclick3"
  11. Android: text = "click to display a detailed progress dialog"/>
  12. </Linearlayout>

3. Code in dialogactivity. java.[Java]View plaincopy

  1. Public class dialogactivity extends activity {
  2. Progressdialog;
  3. /** Called when the activity is first created .*/
  4. @ Override
  5. Public void oncreate (bundle savedinstancestate ){
  6. Super. oncreate (savedinstancestate );
  7. Setcontentview (R. layout. Main );
  8. }
  9. Public void onclick3 (view v ){
  10. Showdialog (1 );
  11. Progressdialog. setprogress (0 );
  12. New thread (New runnable (){
  13. Public void run (){
  14. For (INT I = 1; I <= 15; I ++ ){
  15. Try {
  16. // --- Simulate doing something lengthy ---
  17. Thread. Sleep (1000 );
  18. // --- Update the dialog ---
  19. Progressdialog. incrementprogressby (INT) (100/15 ));
  20. } Catch (interruptedexception e ){
  21. E. printstacktrace ();
  22. }
  23. }
  24. Progressdialog. Dismiss ();
  25. }
  26. }). Start ();
  27. }
  28. @ Override
  29. Protected dialog oncreatedialog (int id ){
  30. Switch (ID ){
  31. Case 1:
  32. Progressdialog = new progressdialog (this );
  33. Progressdialog. seticon (R. drawable. ic_launcher );
  34. Progressdialog. settitle ("downloading files ...");
  35. Progressdialog. setprogressstyle (progressdialog. style_horizontal );
  36. Progressdialog. setbutton (dialoginterface. button_positive, "OK ",
  37. New dialoginterface. onclicklistener (){
  38. Public void onclick (dialoginterface dialog,
  39. Int whichbutton ){
  40. Toast. maketext (getbasecontext (), "OK clicked! ",
  41. Toast. length_short). Show ();
  42. }
  43. });
  44. Progressdialog. setbutton (dialoginterface. button_negative, "cancel ",
  45. New dialoginterface. onclicklistener (){
  46. Public void onclick (dialoginterface dialog,
  47. Int whichbutton ){
  48. Toast. maketext (getbasecontext (), "cancel clicked! ",
  49. Toast. length_short). Show ();
  50. }
  51. });
  52. Return progressdialog;
  53. }
  54. Return NULL;
  55. }
  56. }

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
  1. Progressdialog = new progressdialog (this );
  2. Progressdialog. seticon (R. drawable. ic_launcher );
  3. Progressdialog. settitle ("downloading files ...");
  4. Progressdialog. setprogressstyle (progressdialog. style_horizontal );

Then, set two buttons:[Java]View plaincopy

  1. Progressdialog. setbutton (dialoginterface. button_positive, "OK ",
  2. New dialoginterface. onclicklistener (){
  3. Public void onclick (dialoginterface dialog,
  4. Int whichbutton ){
  5. Toast. maketext (getbasecontext (), "OK clicked! ",
  6. Toast. length_short). Show ();
  7. }
  8. });
  9. Progressdialog. setbutton (dialoginterface. button_negative, "cancel ",
  10. New dialoginterface. onclicklistener (){
  11. Public void onclick (dialoginterface dialog,
  12. Int whichbutton ){
  13. Toast. maketext (getbasecontext (), "cancel clicked! ",
  14. Toast. length_short). Show ();
  15. }
  16. });

Use a thread to display the progress bar status:[Java]View plaincopy

  1. Progressdialog. setprogress (0 );
  2. New thread (New runnable (){
  3. Public void run (){
  4. For (INT I = 1; I <= 15; I ++ ){
  5. Try {
  6. // --- Simulate doing something lengthy ---
  7. Thread. Sleep (1000 );
  8. // --- Update the dialog ---
  9. Progressdialog. incrementprogressby (INT) (100/15 ));
  10. } Catch (interruptedexception e ){
  11. E. printstacktrace ();
  12. }
  13. }
  14. Progressdialog. Dismiss ();
  15. }
  16. }). Start ();

When the progress bar reaches 100%, it is removed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.