Android Learning Note (ix)--More complex progress dialog box

Source: Internet
Author: User

dialog box showing the progress of the operation


1. Add a button to the Activity_main.xml file using the same project you created in the previous article:

    <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"/>

2, add the specific progress bar implementation code in the Mainactivity.java file:

First add the OnClick3 () method:

public void OnClick3 (View v) {showDialog (1);//ID 1, when the callback method Oncreatedialog () is invoked, the ID is passed in to select Case 1. Progressdialog.setprogress (0);//starting from 0 new Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated Method stubfor (int i = 1; i <=; i++) {try {thread.sleep ();p Rogressdialog.incrementprogressby (100/10);//Step 10 } catch (Interruptedexception e) {//Todo:handle exceptione.printstacktrace ();}} Progressdialog.dismiss ();//Destroy Dialog}}). Start ();
Then add the code in the Oncreatedialog () callback method to the id=1 (that is, the code that implements the show progress dialog box):

Case 1:progressdialog = new ProgressDialog (this);p Rogressdialog.seticon (R.drawable.ic_launcher); Progressdialog.settitle ("Downloading files ...");p Rogressdialog.setprogressstyle (progressdialog.style_horizontal )///Set the progress bar style Progressdialog.setbutton (dialoginterface.button_positive, "OK",//Set OK button new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO Auto-generated Method Stubtoast.maketext (Getbasecontext (), "OK clicked!", Toast.length_short). Show ();}); Progressdialog.setbutton (Dialoginterface.button_negative, "Cancel",//Set Cancel button new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubtoast.maketext ( Getbasecontext (), "Cancel clicked!", Toast.length_short). Show ();}); return progressdialog;

3, run a bit, the effect is as follows:



Click here to download the full code ~

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.