码迷,mamicode.com
首页 > 移动开发 > 详细

Android 学习笔记(5)—— ProgressBar

时间:2016-02-22 12:17:42      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

作者:夏至  欢迎转载,也请保留这段申明,谢谢 

    这一章呢,我们来学习进度条ProgressBar,在我们的网页浏览或者其他下载的时候,总有一个缓冲的UI,那这里就是用到我们的Progress,它有两种形式,一种是圆形,一种是水平的。
    先来看看样子
                                  技术分享

我们要实现就是上面的功能。这里我们先看看它的属性:
    · android:max   设置范围大小0到max
    · android:progress 设置当前进度
    · android:secondaryProgress  设置第二进度,比如我们的看视频时的缓冲区域

style=
"?android:attr/ 这里是设置它的模式,有大小和形状,
这里默认是中型圆形
    · progressBarStyleSmall 小型圆形
    · progressBarStyleLarge 大型圆形
    · progressBarStyleHorizontal 水平状态
这里我们获取的方法有:  
       · getMax():返回这个进度条的范围的上限 
       · getProgress():返回进度
       · getSecondaryProgress():返回次要进度

好了,那么,根据上面的图,我们的layout层应该这样子写
  1. <TextView
  2.     android:layout_width="match_parent"
  3.     android:layout_height="wrap_content"
  4.     android:textSize="24sp"
  5.     android:text="小型圆形进度条"
  6. />
  7. <ProgressBar
  8.     android:id="@+id/small"
  9.     android:layout_width="match_parent"
  10.     android:layout_height="wrap_content"
  11.     style="?android:attr/progressBarStyleSmall"
  12. />
  13. <TextView
  14.     android:layout_width="match_parent"
  15.     android:layout_height="wrap_content"
  16.     android:textSize="24sp"
  17.     android:text="中型圆形进度条"
  18. />
  19. <ProgressBar
  20.     android:id="@+id/mid"
  21.     android:layout_width="match_parent"
  22.     android:layout_height="wrap_content"
  23. />
  24. <TextView
  25.     android:layout_width="match_parent"
  26.     android:layout_height="wrap_content"
  27.     android:textSize="24sp"
  28.     android:text="大型圆形进度条"
  29. />
  30. <ProgressBar
  31.     android:id="@+id/large"
  32.     android:layout_width="match_parent"
  33.     android:layout_height="wrap_content"
  34.     style="?android:attr/progressBarStyleLarge"
  35. />

当然这里,我们可以根据上一节学习的开关按钮来实现,圆形进度条的开关功能
  1. <LinearLayout
  2.     android:id="@+id/mylaytout1"
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:orientation="horizontal"
  5.     android:layout_width="wrap_content"
  6.     android:layout_height="wrap_content">
  7. <ToggleButton
  8.         android:id="@+id/tog1"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:textOn="进度条1开"
  12.         android:textOff="进度条1关"
  13.     />
  14. <ToggleButton
  15.         android:id="@+id/tog2"
  16.         android:layout_width="wrap_content"
  17.         android:layout_height="wrap_content"
  18.         android:textOn="进度条2开"
  19.         android:textOff="进度条2关"
  20.     />
  21. <ToggleButton
  22.         android:id="@+id/tog3"
  23.         android:layout_width="wrap_content"
  24.         android:layout_height="wrap_content"
  25.         android:textOn="进度条3开"
  26.         android:textOff="进度条3关"
  27.     />
  28. </LinearLayout>

接下来就是添加两个按钮了,注意上面的线性布局都采用内容包裹,和水平排列的模式。
  1. <TextView
  2.     android:layout_width="match_parent"
  3.     android:layout_height="wrap_content"
  4.     android:textSize="24sp"
  5.     android:text="水平形进度条"
  6. />
  7. <ProgressBar
  8.     android:id="@+id/pbar"
  9.     android:layout_width="match_parent"
  10.     android:layout_height="wrap_content"
  11.     style="?android:attr/progressBarStyleHorizontal"
  12.     android:max="100"
  13.     android:progress="10"
  14. />
  15. <ProgressBar
  16.     android:id="@+id/pbar2"
  17.     android:layout_width="match_parent"
  18.     android:layout_height="wrap_content"
  19.     style="?android:attr/progressBarStyleHorizontal"
  20.     android:max="100"
  21.     android:progress="10"
  22.     android:secondaryProgress="20"
  23. />
  24. <Button
  25.     android:id="@+id/btn1"
  26.     android:layout_width="match_parent"
  27.     android:layout_height="wrap_content"
  28.     android:text="增加进度"/>
  29. <Button
  30.     android:id="@+id/btn2"
  31.     android:layout_width="match_parent"
  32.     android:layout_height="wrap_content"
  33.     android:text="减少进度"/>

这样我们就写好了布局了,接下来就是主活动的程序了。先把我们要用到的方法包裹起来,和定义好类变量
  1. public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
  2.     private ProgressBar small,mid,large,pbar,pbar2;
  3.     private ToggleButton one,two,three;
  4.     private Button btn1,btn2;

然后开始实例化了。
  1. small = (ProgressBar)findViewById(R.id.small);
  2. mid = (ProgressBar)findViewById(R.id.mid);
  3. large = (ProgressBar)findViewById(R.id.large);
  4. pbar = (ProgressBar)findViewById(R.id.pbar);
  5. pbar2 = (ProgressBar)findViewById(R.id.pbar2);
  6. one = (ToggleButton)findViewById(R.id.tog1);
  7. two = (ToggleButton)findViewById(R.id.tog2);
  8. three = (ToggleButton)findViewById(R.id.tog3);
  9. btn1 = (Button)findViewById(R.id.btn1);
  10. btn2 = (Button)findViewById(R.id.btn2);
  11. one.setOnCheckedChangeListener(this);
  12. two.setOnCheckedChangeListener(this);
  13. three.setOnCheckedChangeListener(this);
  14. btn1.setOnClickListener(this);
  15. btn2.setOnClickListener(this);

这里我们用到两个方法,一个是开关方法
  1. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  2.     if(one.isChecked()) small.setVisibility(View.VISIBLE); //开进度条1
  3.        else small.setVisibility(View.GONE);
  4.  if(two.isChecked()) mid.setVisibility(View.VISIBLE); //开进度条2
  5.        else mid.setVisibility(View.GONE);
  6. if(three.isChecked()) large.setVisibility(View.VISIBLE); //开进度条3
  7.         else large.setVisibility(View.GONE);
  8. }

另一个是按键功能
  1. public void onClick(View v) {
  2. int progress = 0;
  3. switch (v.getId()){
  4.         case R.id.btn1:
  5.             pbar.setProgress((int)(pbar.getProgress()*1.5));
  6.             pbar2.setProgress((int)(pbar2.getProgress()*1.5));
  7.             pbar2.setSecondaryProgress((int) (pbar2.getProgress()*1.5));
  8. break;
  9. case R.id.btn2:
  10. progress = pbar.getProgress();
  11. if(progress <=2) progress = 2; //不能小于1,不然pbar.getProgress()*1.5)就没意义了
  12.             else progress *= 0.9;
  13.             pbar.setProgress(progress);
  14.             pbar2.setProgress(progress);
  15.             pbar2.setSecondaryProgress(progress);
  16. break;
  17.     }
  18. }

这样就完成了,效果如图:
技术分享

如有错误,欢迎指出,如果喜欢,欢迎收藏!
























Android 学习笔记(5)—— ProgressBar

标签:

原文地址:http://www.cnblogs.com/shaorui/p/5206543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!