Android 程式开发:(十)基本控件 —— 10.3 ProgressBar

当执行某些正在处理的任务时,ProgressBar提供了一个可视化的反馈。例如,你在从web服务器下载数据,然后需要更新下载的状态。在这种情况下,ProgressBar就是一个很好的选择。下面的例子,展示如何去使用ProgressBar。

1、创建一个工程,BasicViews2。

2、main.xml中的代码。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <ProgressBarandroid:id="@+id/progressbar"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"/>
  9. </LinearLayout>
3、Basic2Activity.java中的代码。
  1. packagenet.learn2develop.BasicViews2;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.os.Handler;
  5. importandroid.view.View;
  6. importandroid.widget.ProgressBar;
  7. publicclassBasicViews2ActivityextendsActivity{
  8. staticintprogress;
  9. ProgressBarprogressBar;
  10. intprogressStatus=0;
  11. Handlerhandler=newHandler();
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. progress=0;
  18. progressBar=(ProgressBar)findViewById(R.id.progressbar);
  19. //---dosomeworkinbackgroundthread---
  20. newThread(newRunnable()
  21. {
  22. publicvoidrun()
  23. {
  24. //?dosomeworkhere?
  25. while(progressStatus<10)
  26. {
  27. progressStatus=doSomeWork();
  28. }
  29. //---hidestheprogressbar---
  30. handler.post(newRunnable()
  31. {
  32. publicvoidrun()
  33. {
  34. //---0-VISIBLE;4-INVISIBLE;8-GONE---
  35. progressBar.setVisibility(View.GONE);
  36. }
  37. });
  38. }
  39. //---dosomelongrunningworkhere---
  40. privateintdoSomeWork()
  41. {
  42. try{
  43. //---simulatedoingsomework---
  44. Thread.sleep(500);
  45. }catch(InterruptedExceptione)
  46. {
  47. e.printStackTrace();
  48. }
  49. return++progress;
  50. }
  51. }).start();
  52. }
  53. }
4、F11调试,会看见ProgressBar的动画,5秒之后,动画消失。



接下来展示如何自定义ProgressBar的样式。

1、使用之前的例子,修改main.xml。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <ProgressBarandroid:id="@+id/progressbar"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. style="@android:style/Widget.ProgressBar.Horizontal"/>
  10. </LinearLayout>

2、Basic2Activity.java中的代码。

  1. packagenet.learn2develop.BasicViews2;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.os.Handler;
  5. importandroid.view.View;
  6. importandroid.widget.ProgressBar;
  7. publicclassBasicViews2ActivityextendsActivity{
  8. staticintprogress;
  9. ProgressBarprogressBar;
  10. intprogressStatus=0;
  11. Handlerhandler=newHandler();
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. progress=0;
  18. progressBar=(ProgressBar)findViewById(R.id.progressbar);
  19. progressBar.setMax(200);
  20. //---dosomeworkinbackgroundthread---
  21. newThread(newRunnable()
  22. {
  23. publicvoidrun()
  24. {
  25. //?dosomeworkhere?
  26. while(progressStatus<100)
  27. {
  28. progressStatus=doSomeWork();
  29. //?Updatetheprogressbar?
  30. handler.post(newRunnable()
  31. {
  32. publicvoidrun(){
  33. progressBar.setProgress(progressStatus);
  34. }
  35. });
  36. }
  37. //---hidestheprogressbar---
  38. handler.post(newRunnable()
  39. {
  40. publicvoidrun()
  41. {
  42. //---0-VISIBLE;4-INVISIBLE;8-GONE---
  43. progressBar.setVisibility(View.GONE);
  44. }
  45. });
  46. }
  47. //---dosomelongrunningworkhere---
  48. privateintdoSomeWork()
  49. {
  50. try{
  51. //---simulatedoingsomework---
  52. Thread.sleep(50);
  53. }catch(InterruptedExceptione)
  54. {
  55. e.printStackTrace();
  56. }
  57. return++progress;
  58. }
  59. }).start();
  60. }
  61. }
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值