Android-handler Update View plus threads

Source: Internet
Author: User

Code

 PackageNet.blogjava.mobile;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.widget.ProgressBar; Public classMainextendsactivity{PrivateProgressBar progressBar1; PrivateProgressBar progressBar2; PrivateHandler Handler =NewHandler (); Private intCount1 = 0; Private intCount2 = 0; PrivateRunnable doUpdateProgressBar1 =NewRunnable () {@Override Public voidrun () { for(count1 = 0; count1 <= progressbar1.getmax (); count1++) {Handler.post (NewRunnable () {@Override Public voidrun () {progressbar1.setprogress (count1);            }                });    }        }    }; PrivateRunnable doUpdateProgressBar2 =NewRunnable () {@Override Public voidrun () { for(count2 = 0; Count2 <= progressbar2.getmax (); count2++) {Handler.post (NewRunnable () {@Override Public voidrun () {progressbar2.setprogress (count2);            }                });    }        }    }; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); ProgressBar1=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); ProgressBar2=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR2); Thread Thread1=NewThread (doUpdateProgressBar1, "Thread1");        Thread1.start (); Thread thread2=NewThread (DOUPDATEPROGRESSBAR2, "Thread2");    Thread2.start (); }}

Layout

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <ProgressBarAndroid:id= "@+id/progressbar1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"style= "? Android:attr/progressbarstylehorizontal"Android:max= "20000"Android:layout_margintop= "10DP" />    <ProgressBarAndroid:id= "@+id/progressbar2"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"style= "? Android:attr/progressbarstylehorizontal"Android:max= "10000"Android:layout_margintop= "10DP"  /></LinearLayout>

Android-handler Update View plus threads

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.