当执行某些正在处理的任务时,ProgressBar提供了一个可视化的反馈。例如,你在从web服务器下载数据,然后需要更新下载的状态。在这种情况下,ProgressBar就是一个很好的选择。下面的例子,展示如何去使用ProgressBar。
1、创建一个工程,BasicViews2。
2、main.xml中的代码。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <ProgressBarandroid:id="@+id/progressbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- packagenet.learn2develop.BasicViews2;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.view.View;
- importandroid.widget.ProgressBar;
- publicclassBasicViews2ActivityextendsActivity{
- staticintprogress;
- ProgressBarprogressBar;
- intprogressStatus=0;
- Handlerhandler=newHandler();
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- progress=0;
- progressBar=(ProgressBar)findViewById(R.id.progressbar);
- //---dosomeworkinbackgroundthread---
- newThread(newRunnable()
- {
- publicvoidrun()
- {
- //?dosomeworkhere?
- while(progressStatus<10)
- {
- progressStatus=doSomeWork();
- }
- //---hidestheprogressbar---
- handler.post(newRunnable()
- {
- publicvoidrun()
- {
- //---0-VISIBLE;4-INVISIBLE;8-GONE---
- progressBar.setVisibility(View.GONE);
- }
- });
- }
- //---dosomelongrunningworkhere---
- privateintdoSomeWork()
- {
- try{
- //---simulatedoingsomework---
- Thread.sleep(500);
- }catch(InterruptedExceptione)
- {
- e.printStackTrace();
- }
- return++progress;
- }
- }).start();
- }
- }
接下来展示如何自定义ProgressBar的样式。
1、使用之前的例子,修改main.xml。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <ProgressBarandroid:id="@+id/progressbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="@android:style/Widget.ProgressBar.Horizontal"/>
- </LinearLayout>
2、Basic2Activity.java中的代码。
- packagenet.learn2develop.BasicViews2;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.view.View;
- importandroid.widget.ProgressBar;
- publicclassBasicViews2ActivityextendsActivity{
- staticintprogress;
- ProgressBarprogressBar;
- intprogressStatus=0;
- Handlerhandler=newHandler();
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- progress=0;
- progressBar=(ProgressBar)findViewById(R.id.progressbar);
- progressBar.setMax(200);
- //---dosomeworkinbackgroundthread---
- newThread(newRunnable()
- {
- publicvoidrun()
- {
- //?dosomeworkhere?
- while(progressStatus<100)
- {
- progressStatus=doSomeWork();
- //?Updatetheprogressbar?
- handler.post(newRunnable()
- {
- publicvoidrun(){
- progressBar.setProgress(progressStatus);
- }
- });
- }
- //---hidestheprogressbar---
- handler.post(newRunnable()
- {
- publicvoidrun()
- {
- //---0-VISIBLE;4-INVISIBLE;8-GONE---
- progressBar.setVisibility(View.GONE);
- }
- });
- }
- //---dosomelongrunningworkhere---
- privateintdoSomeWork()
- {
- try{
- //---simulatedoingsomework---
- Thread.sleep(50);
- }catch(InterruptedExceptione)
- {
- e.printStackTrace();
- }
- return++progress;
- }
- }).start();
- }
- }
4、当ProgressBar的进度到达50%的时候,它就会消失。
附:
ProgressBar的样式列表。
- Widget.ProgressBar.Horizontal
- Widget.ProgressBar.Small
- Widget.ProgressBar.Large
- Widget.ProgressBar.Inverse
- Widget.ProgressBar.Small.Inverse
- Widget.ProgressBar.Large.Inverse