当执行某些正在处理的任务时,ProgressBar提供了一个可视化的反馈。例如,你在从web服务器下载数据,然后需要更新下载的状态。在这种情况下,ProgressBar就是一个很好的选择。下面的例子,展示如何去使用ProgressBar。
1、创建一个工程,BasicViews2。
2、main.xml中的代码。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>3、Basic2Activity.java中的代码。 package net.learn2develop.BasicViews2; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.ProgressBar; public class BasicViews2Activity extends Activity { static int progress; ProgressBar progressBar; int progressStatus = 0; Handler handler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progress = 0; progressBar = (ProgressBar) findViewById(R.id.progressbar); //---do some work in background thread--- new Thread(new Runnable() { public void run() { //?do some work here? while (progressStatus < 10) { progressStatus = doSomeWork(); } //---hides the progress bar--- handler.post(new Runnable() { public void run() { //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE--- progressBar.setVisibility(View.GONE); } }); } //---do some long running work here--- private int doSomeWork() { try { //---simulate doing some work--- Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return ++progress; } }).start(); } }4、F11调试,会看见ProgressBar的动画,5秒之后,动画消失。接下来展示如何自定义ProgressBar的样式。
1、使用之前的例子,修改main.xml。
2、Basic2Activity.java中的代码。
package net.learn2develop.BasicViews2; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.ProgressBar; public class BasicViews2Activity extends Activity { static int progress; ProgressBar progressBar; int progressStatus = 0; Handler handler = new Handler(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progress = 0; progressBar = (ProgressBar) findViewById(R.id.progressbar); progressBar.setMax(200); //---do some work in background thread--- new Thread(new Runnable() { public void run() { //?do some work here? while (progressStatus < 100) { progressStatus = doSomeWork(); //?Update the progress bar? handler.post(new Runnable() { public void run() { progressBar.setProgress(progressStatus); } }); } //---hides the progress bar--- handler.post(new Runnable() { public void run() { //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE--- progressBar.setVisibility(View.GONE); } }); } //---do some long running work here--- private int doSomeWork() { try { //---simulate doing some work--- Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } return ++progress; } }).start(); } }3、F11调试。
4、当ProgressBar的进度到达50%的时候,它就会消失。
附:
ProgressBar的样式列表。
- Widget.ProgressBar.Horizontal
- Widget.ProgressBar.Small
- Widget.ProgressBar.Large
- Widget.ProgressBar.Inverse
- Widget.ProgressBar.Small.Inverse
- Widget.ProgressBar.Large.Inverse