当前位置:   article > 正文

【安卓学习笔记】Android Studio第9课——进度条ProgressBar、SeekBar和RatingBar_android中进度条用法

android中进度条用法

总体上Android默认常用的进度条分为四种:

1、垂直风格的圈状ProgressBar


这种转圈形式的进度条可以一般用作模糊指示,换句话说这个进度条无法体现出来当前的精确进度,只能傻傻的转圈。

在设置上有:

  1. style="?android:progressBarStyleLarge"//大
  2. style="?android:progressBarStyleSmall"//小
这两个属性比较常见,其次还有inverse(反转),效果好像有跟没有一样

2、水平风格的条状ProgressBar

条状的进度条就可以设置和体现出精确的进度。
在属性里面需要设置成水平方向:
style="?android:progressBarStyleHorizontal"
2.1常用到的参数有max、progress、secondaryProgress,分别代表了进度条的最大值,当前的进度值和第二进度值。这里解释下secondaryProgress,这里打个比方:假如一个任务需要分为步骤依次的完成,第一个步骤是建立在第二个步骤基础上的,那么这种情况就很适合用progress表示第一个步骤完成的进度,用secondaryProgress表示第二步骤完成的进度。
2.2常用到方法有
  1. progressBar1.setMax(200);//设置最大值为200
  2. progressBar1.setProgress(100);//设置第一进度为100
  3. progressBar1.setSecondaryProgress(150);//设置第二进度为150
  4. /**
  5. * 获得进度条是否为模糊进度条
  6. * 返回值:ture - 模糊进度条
  7. * true - 精确进度条
  8. */
  9. boolean isIndeterminate = progressBar1.isIndeterminate();
  10. progressBar1.incrementProgressBy(10);//相对主进度增加10
  11. progressBar1.incrementSecondaryProgressBy(10);//相对第二进度增加10
3、可拖拽的SeekBar


SeekBar是ProgressBar的子类,跟ProgressBar的水平风格很相似,也有max、progress和secondaryProgress三个重要的参数
常用的设置方法如下:
  1. seekBar.setMax(100);
  2. seekBar.setProgress(0);
  3. seekBar.setSecondaryProgress(10);
其次他还对应的有自己的监听器SeekBar.OnSeekBarChangeListener
在监听器里面有三个触发时间,分别对应进度条改变、进度条触摸改变的开始和进度条触摸改变的结束。
  1. @Override
  2. /**
  3. * seekBar改变出发的监听器
  4. * fromUser : 是否是用户手动操作的
  5. */
  6. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  7. }
  8. @Override
  9. /**
  10. * 用户开始拖拽的时候出发的监听器
  11. */
  12. public void onStartTrackingTouch(SeekBar seekBar) {
  13. }
  14. @Override
  15. /**
  16. * 用户停止拖拽的时候出发的监听器
  17. */
  18. public void onStopTrackingTouch(SeekBar seekBar) {
  19. System.out.println("SeekBar--> progress = " + seekBar.getProgress());
  20. }
所以根据软件和应用的需求可以自己定义需要用到哪一个。

4、评分用的RatingBar


RatingBar也是ProgressBar的子类,这个风格常用来做评分或者评等级用。常用到参数有星星个数、步进。用法如下:
  1. ratingBar.setNumStars(5);
  2. ratingBar.setStepSize((float) 0.5);
也有对应的监听器OnRatingBarChangeListener,方法都很相似,就不贴单独贴代码。

程序只是用来测试用,肯定漏洞百出,下面给出测试的Activity并贴上全部代码:


XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".Lesson8_Activity">
  8. <ProgressBar
  9. android:id="@+id/L8_progressbar1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_centerHorizontal="true"
  13. style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"/>
  14. <ProgressBar
  15. android:id="@+id/L8_progressbar2"
  16. android:layout_below="@+id/L8_progressbar1"
  17. android:layout_width="200dp"
  18. android:layout_height="wrap_content"
  19. android:layout_centerHorizontal="true"
  20. android:layout_marginTop="10dp"
  21. style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
  22. android:max="200"
  23. android:progress="100"
  24. android:secondaryProgress="150"
  25. />
  26. <SeekBar
  27. android:id="@+id/L8_progressbar3"
  28. android:layout_width="200dp"
  29. android:layout_height="wrap_content"
  30. android:layout_below="@+id/L8_progressbar2"
  31. android:layout_centerHorizontal="true"
  32. android:layout_marginTop="10dp"
  33. android:max="100"
  34. android:progress="20"
  35. android:secondaryProgress="50"
  36. />
  37. <RatingBar
  38. android:id="@+id/L8_progressbar4"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_below="@+id/L8_progressbar3"
  42. android:layout_centerHorizontal="true"
  43. android:layout_marginTop="10dp"
  44. />
  45. <RadioGroup
  46. android:id="@+id/L8_radioGroup"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_below="@+id/L8_progressbar4"
  50. android:layout_centerHorizontal="true"
  51. android:layout_marginTop="10dp"
  52. android:gravity="fill_horizontal">
  53. <RadioButton
  54. android:id="@+id/L8_radioButton1"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:text="圈状"/>
  58. <RadioButton
  59. android:id="@+id/L8_radioButton2"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:text="条状"/>
  63. <RadioButton
  64. android:id="@+id/L8_radioButton3"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:text="SeekBar"/>
  68. <RadioButton
  69. android:id="@+id/L8_radioButton4"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:text="RatingBar"/>
  73. </RadioGroup>
  74. <Button
  75. android:id="@+id/L8_button1"
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content"
  78. android:layout_below="@+id/L8_radioGroup"
  79. android:layout_marginLeft="30dp"
  80. android:text="增加第一进度"/>
  81. <Button
  82. android:id="@+id/L8_button2"
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:text="增加第二进度"
  86. android:layout_below="@+id/L8_radioGroup"
  87. android:layout_alignParentRight="true"
  88. android:layout_marginRight="30dp"/>
  89. </RelativeLayout>
Activity.java
  1. package com.example.urien.secondapp;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.ProgressBar;
  7. import android.widget.RadioButton;
  8. import android.widget.RadioGroup;
  9. import android.widget.RatingBar;
  10. import android.widget.SeekBar;
  11. import static android.widget.RatingBar.*;
  12. public class Lesson8_Activity extends AppCompatActivity {
  13. //第一步:声明控件
  14. private ProgressBar progressBar1;
  15. private ProgressBar progressBar2;
  16. private SeekBar seekBar;
  17. private RatingBar ratingBar;
  18. private Button button1;
  19. private Button button2;
  20. private RadioGroup radioGroup;
  21. private RadioButton radioButton1;
  22. private RadioButton radioButton2;
  23. private RadioButton radioButton3;
  24. private RadioButton radioButton4;
  25. int value_radioButton;
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_lesson8_);
  30. //第二步:找到控件
  31. progressBar1 = findViewById(R.id.L8_progressbar1);
  32. progressBar2 = findViewById(R.id.L8_progressbar2);
  33. seekBar = findViewById(R.id.L8_progressbar3);
  34. ratingBar = findViewById(R.id.L8_progressbar4);
  35. button1 = findViewById(R.id.L8_button1);
  36. button2 = findViewById(R.id.L8_button2);
  37. radioGroup = findViewById(R.id.L8_radioGroup);
  38. radioButton1 = findViewById(R.id.L8_radioButton1);
  39. radioButton2 = findViewById(R.id.L8_radioButton2);
  40. radioButton3 = findViewById(R.id.L8_radioButton3);
  41. radioButton4 = findViewById(R.id.L8_radioButton4);
  42. //第四步:绑定监听器
  43. seekBar.setOnSeekBarChangeListener(new seekBarListener());
  44. ratingBar.setOnRatingBarChangeListener(new ratingBarListener());
  45. radioGroup.setOnCheckedChangeListener(new radioGruopListener());
  46. button1.setOnClickListener(new buttonListener());
  47. button2.setOnClickListener(new buttonListener());
  48. /* progressBar1.setMax(200);//设置最大值为200
  49. progressBar1.setProgress(100);//设置第一进度为100
  50. progressBar1.setSecondaryProgress(150);//设置第二进度为200
  51. int valueProgress1 = progressBar1.getProgress();//得到第一进度的值
  52. int valuesecondProgress1 = progressBar1.getSecondaryProgress();//得到第二进度的值
  53. *//**
  54. * 获得进度条是否为模糊进度条
  55. * 返回值:ture - 模糊进度条
  56. * true - 精确进度条
  57. *//*
  58. boolean isIndeterminate = progressBar1.isIndeterminate();
  59. progressBar1.incrementProgressBy(10);//相对主进度增加10
  60. progressBar1.incrementSecondaryProgressBy(10);//相对第二进度增加10*/
  61. }
  62. //第三步:实现监听器接口
  63. /**
  64. * seekBar监听器
  65. */
  66. class seekBarListener implements SeekBar.OnSeekBarChangeListener {
  67. @Override
  68. /**
  69. * seekBar改变出发的监听器
  70. * fromUser : 是否是用户手动操作的
  71. */
  72. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  73. }
  74. @Override
  75. /**
  76. * 用户开始拖拽的时候出发的监听器
  77. */
  78. public void onStartTrackingTouch(SeekBar seekBar) {
  79. }
  80. @Override
  81. /**
  82. * 用户停止拖拽的时候出发的监听器
  83. */
  84. public void onStopTrackingTouch(SeekBar seekBar) {
  85. System.out.println("SeekBar--> progress = " + seekBar.getProgress());
  86. }
  87. }
  88. /**
  89. * ratioGroup监听器
  90. */
  91. class radioGruopListener implements RadioGroup.OnCheckedChangeListener {
  92. @Override
  93. public void onCheckedChanged(RadioGroup group, int checkedId) {
  94. if (checkedId == R.id.L8_radioButton1) {
  95. value_radioButton = 1;
  96. } else if (checkedId == R.id.L8_radioButton2) {
  97. value_radioButton = 2;
  98. } else if (checkedId == R.id.L8_radioButton3) {
  99. value_radioButton = 3;
  100. } else if (checkedId == R.id.L8_radioButton4) {
  101. value_radioButton = 4;
  102. }
  103. }
  104. }
  105. /**
  106. * Button监听器
  107. */
  108. class buttonListener implements View.OnClickListener {
  109. @Override
  110. public void onClick(View v) {
  111. //ProgressBar tempProgressBar;
  112. if (v.getId() == R.id.L8_button1) {
  113. switch (value_radioButton) {
  114. case 1:
  115. // progressBar1.incrementProgressBy(10);
  116. break;
  117. case 2:
  118. progressBar2.incrementProgressBy(10);
  119. break;
  120. case 3:
  121. seekBar.incrementProgressBy(10);
  122. break;
  123. case 4:
  124. ratingBar.incrementProgressBy(10);
  125. break;
  126. }
  127. } else if (v.getId() == R.id.L8_button2) {
  128. switch (value_radioButton) {
  129. case 1:
  130. progressBar1.incrementSecondaryProgressBy(10);
  131. break;
  132. case 2:
  133. progressBar2.incrementSecondaryProgressBy(10);
  134. break;
  135. case 3:
  136. seekBar.incrementSecondaryProgressBy(10);
  137. break;
  138. case 4:
  139. ratingBar.incrementSecondaryProgressBy(10);
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * RatingBar监听器
  147. */
  148. class ratingBarListener implements OnRatingBarChangeListener {
  149. @Override
  150. /**
  151. * 这里的formUser可以用来判定是不是用户手动改变
  152. */
  153. public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
  154. System.out.println("RatingBar--> rating = " + rating + " formUser " + fromUser);
  155. }
  156. }
  157. /**
  158. * 初始化函数
  159. */
  160. private void L8_AvtivityConfiguration() {
  161. progressBar1.setMax(100);
  162. progressBar1.setProgress(0);
  163. progressBar1.setSecondaryProgress(10);
  164. progressBar2.setMax(100);
  165. progressBar2.setProgress(0);
  166. progressBar2.setSecondaryProgress(10);
  167. seekBar.setMax(100);
  168. seekBar.setProgress(0);
  169. seekBar.setSecondaryProgress(10);
  170. ratingBar.setNumStars(5);
  171. ratingBar.setStepSize((float) 0.5);
  172. radioButton1.setChecked(true);
  173. }
  174. }
By Urien 2018年6月3日 17:57:28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/474093
推荐阅读
相关标签
  

闽ICP备14008679号