Android learning notes: ProgressBar case study, androidprogressbar

Source: Internet
Author: User

Android learning notes: ProgressBar case study, androidprogressbar


(1)

<RelativeLayout 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: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_margin "tools: context = ". mainActivity "> <TextView android: id =" @ + id/textView1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" progress bar usage: "/> <ProgressBar android: id =" @ + id/progressBar1 "style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignLeft =" @ + id/button1 "android: layout_alignRight = "@ + id/button1" android: layout_below = "@ + id/textView1" android: layout_marginTop = "30dp"/> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/textView1" android: layout_below = "@ + id/progressBar1" android: layout_marginLeft = "74dp" android: layout_marginTop = "146dp" android: text = "Download image"/> </RelativeLayout>

(2)

Package com. example. progressbar; import android. app. activity; import android. OS. asyncTask; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. progressBar; public class MainActivity extends Activity {private Button button1; private ProgressBar progressBar1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) this. findViewById (R. id. button1); progressBar1 = (ProgressBar) this. findViewById (R. id. progressBar1); progressBar1.setMax (100); // set the maximum value to 100button1. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {new mytask(cmd.exe cute () ;}});} class MyTask extends AsyncTask <Void, Integer, void >{@ Overrideprotected void onProgressUpdate (Integer... values) {super. onProgressUpdate (values); progressBar1.setProgress (values [0]);} @ Overrideprotected Void doInBackground (Void... params) {int I = 1; while (I <= 100) {try {Thread. sleep (500);} catch (Exception e) {// TODO: handle exception} publishProgress (I); I ++;} return null ;}} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

(3) demonstration effect


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.