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