Android Learning notes: progress bar ProgressBar and Threads

Source: Internet
Author: User

ProgressBar is the progress bar, the indicator in Android, look first

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6C/F1/wKioL1VYTsiCsoKUAABbM7chj2k961.jpg "title=" QQ picture 20150517161048.png "alt=" Wkiol1vytsicsokuaabbm7chj2k961.jpg "/>



The horizontal progress bar can be seen in two colors, the light is secondaryprogress, can be used as a demonstration of intermediate progress.

ProgressBar are available in a variety of styles, with horizontal and circle circles. If you need to indicate that the progress can be used horizontally, other styles can be found in the Android API documentation

Other progress bar styles provided by the system include:

    • Widget.ProgressBar.Horizontal

    • Widget.ProgressBar.Small

    • Widget.ProgressBar.Large

    • Widget.ProgressBar.Inverse

    • Widget.ProgressBar.Small.Inverse

    • Widget.ProgressBar.Large.Inverse

First look at the layout file Activity_main.xml

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"     xmlns: tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"      android:layout_height= "match_parent"     android:orientation= "vertical"     tools:context= "Com.jam.testprogressbar.MainActivity"  >     <textview        android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"          android:text= "@string/hello_world"  />    <ProgressBar         android:id= "@+id/id_firstbar_id"          android:layout_width= "150DP"         android:layout_height= "Wrap_ Content "  &nbsP;     style= "@android: Style/widget.progressbar.horizontal"          android:visibility= "Gone"  />    <ProgressBar         android:id= "@+id/id_spinningbar_id"          android:layout_width= "150DP"         android:layout_ height= "Wrap_content"          android:visibility= "Gone"/>         <Button          android:id= "@+id/id_buttonfirst"         android:layout_width= " Wrap_content "        android:layout_height=" Wrap_content "         android:text= "AddFirst"/>             <butTon         android:id= "@+id/id_buttonsecond"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:text= " Addsecond "/></linearlayout>

Simple layout, to modify the ProgressBar, you only need to change the style of the content.


Then there is the content of Mainactivity.java:

Package com.jam.testprogressbar;import android.app.activity;import android.os.bundle;import  android.view.view;import android.view.view.onclicklistener;import android.widget.button;import  android.widget.progressbar;public class mainactivity extends activity {// Declaring a control private progressbar firstprogressbar;private progressbar secondprogressbar;private  Button buttonFirst;private Button buttonSecond;private int i = 0; private int j = 0;     @Override     protected  Void oncreate (bundle savedinstancestate)  {         Super.oncreate (savedinstancestate);         setcontentview ( R.layout.activity_main);                 firstProgressBar =  (progrEssbar)  findviewbyid (r.id.id_firstbar_id);         secondprogressbar =  (ProgressBar)  findviewbyid (r.id.id_spinningbar_id);         buttonFirst =  (Button)  findviewbyid (R.id.id_buttonfirst);         buttonSecond =  (Button)  findviewbyid (R.id.id_buttonsecond);                 /**          *  binds the first button to an anonymous inner class           *  when i==0, show two progressbar         * i 100  , add 10 per press.          *  when I is over 100, hide two progressbar          */        buttonfirst.setonclicklistener (New onclickliStener ()  {@Overridepublic  void onclick (view v)  {if (i == 0)  { Firstprogressbar.setvisibility (view.visible); secondprogressbar.setvisibility (view.visible); i += 10;j  = i;firstprogressbar.setprogress (i); Firstprogressbar.setsecondaryprogress (j);  else if ( i < 100) {i += 10;j = i;firstprogressbar.setprogress ( i);}  else {i = 0;j = i;firstprogressbar.setvisibility (View.GONE); Secondprogressbar.setvisibility (View.gone);}}  });                 /**          *  the second button to set the secondaryprogress.          */         Buttonsecond.setonclicklistener (New onclicklistener ()  {@Overridepublic  void onclick (View &NBSP;V)  {if (i != 0)  {j += 10;firstprogressbar.setsecondaryprogress (j);}});     }}

The implementation of the progress bar code is simple, in the API documentation there are examples and other specific methods, not described.




Note: In principle, except for the main thread, other threads do not allow access to the main thread's UI, except ProgressBar

Android Learning notes: progress bar ProgressBar and 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.