赞
踩
总体上Android默认常用的进度条分为四种:
1、垂直风格的圈状ProgressBar
这种转圈形式的进度条可以一般用作模糊指示,换句话说这个进度条无法体现出来当前的精确进度,只能傻傻的转圈。
在设置上有:
- style="?android:progressBarStyleLarge"//大
- style="?android:progressBarStyleSmall"//小
这两个属性比较常见,其次还有inverse(反转),效果好像有跟没有一样
style="?android:progressBarStyleHorizontal"
2.1常用到的参数有max、progress、secondaryProgress,分别代表了进度条的最大值,当前的进度值和第二进度值。这里解释下secondaryProgress,这里打个比方:假如一个任务需要分为步骤依次的完成,第一个步骤是建立在第二个步骤基础上的,那么这种情况就很适合用progress表示第一个步骤完成的进度,用secondaryProgress表示第二步骤完成的进度。
- progressBar1.setMax(200);//设置最大值为200
- progressBar1.setProgress(100);//设置第一进度为100
- progressBar1.setSecondaryProgress(150);//设置第二进度为150
-
- /**
- * 获得进度条是否为模糊进度条
- * 返回值:ture - 模糊进度条
- * true - 精确进度条
- */
- boolean isIndeterminate = progressBar1.isIndeterminate();
-
- progressBar1.incrementProgressBy(10);//相对主进度增加10
- progressBar1.incrementSecondaryProgressBy(10);//相对第二进度增加10
3、可拖拽的SeekBar
- seekBar.setMax(100);
- seekBar.setProgress(0);
- seekBar.setSecondaryProgress(10);
其次他还对应的有自己的监听器SeekBar.OnSeekBarChangeListener
所以根据软件和应用的需求可以自己定义需要用到哪一个。
@Override /** * seekBar改变出发的监听器 * fromUser : 是否是用户手动操作的 */ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override /** * 用户开始拖拽的时候出发的监听器 */ public void onStartTrackingTouch(SeekBar seekBar) { } @Override /** * 用户停止拖拽的时候出发的监听器 */ public void onStopTrackingTouch(SeekBar seekBar) { System.out.println("SeekBar--> progress = " + seekBar.getProgress()); }
- ratingBar.setNumStars(5);
- ratingBar.setStepSize((float) 0.5);
也有对应的监听器OnRatingBarChangeListener,方法都很相似,就不贴单独贴代码。
程序只是用来测试用,肯定漏洞百出,下面给出测试的Activity并贴上全部代码:
、
XML:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".Lesson8_Activity">
-
- <ProgressBar
- android:id="@+id/L8_progressbar1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"/>
-
- <ProgressBar
- android:id="@+id/L8_progressbar2"
- android:layout_below="@+id/L8_progressbar1"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="10dp"
- style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
- android:max="200"
- android:progress="100"
- android:secondaryProgress="150"
- />
-
- <SeekBar
- android:id="@+id/L8_progressbar3"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_below="@+id/L8_progressbar2"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="10dp"
- android:max="100"
- android:progress="20"
- android:secondaryProgress="50"
- />
- <RatingBar
- android:id="@+id/L8_progressbar4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/L8_progressbar3"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="10dp"
- />
- <RadioGroup
- android:id="@+id/L8_radioGroup"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/L8_progressbar4"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="10dp"
- android:gravity="fill_horizontal">
- <RadioButton
- android:id="@+id/L8_radioButton1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="圈状"/>
- <RadioButton
- android:id="@+id/L8_radioButton2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="条状"/>
- <RadioButton
- android:id="@+id/L8_radioButton3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="SeekBar"/>
- <RadioButton
- android:id="@+id/L8_radioButton4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="RatingBar"/>
- </RadioGroup>
- <Button
- android:id="@+id/L8_button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/L8_radioGroup"
- android:layout_marginLeft="30dp"
- android:text="增加第一进度"/>
- <Button
- android:id="@+id/L8_button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="增加第二进度"
- android:layout_below="@+id/L8_radioGroup"
- android:layout_alignParentRight="true"
- android:layout_marginRight="30dp"/>
- </RelativeLayout>

Activity.java
By Urien 2018年6月3日 17:57:28
package com.example.urien.secondapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RatingBar; import android.widget.SeekBar; import static android.widget.RatingBar.*; public class Lesson8_Activity extends AppCompatActivity { //第一步:声明控件 private ProgressBar progressBar1; private ProgressBar progressBar2; private SeekBar seekBar; private RatingBar ratingBar; private Button button1; private Button button2; private RadioGroup radioGroup; private RadioButton radioButton1; private RadioButton radioButton2; private RadioButton radioButton3; private RadioButton radioButton4; int value_radioButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lesson8_); //第二步:找到控件 progressBar1 = findViewById(R.id.L8_progressbar1); progressBar2 = findViewById(R.id.L8_progressbar2); seekBar = findViewById(R.id.L8_progressbar3); ratingBar = findViewById(R.id.L8_progressbar4); button1 = findViewById(R.id.L8_button1); button2 = findViewById(R.id.L8_button2); radioGroup = findViewById(R.id.L8_radioGroup); radioButton1 = findViewById(R.id.L8_radioButton1); radioButton2 = findViewById(R.id.L8_radioButton2); radioButton3 = findViewById(R.id.L8_radioButton3); radioButton4 = findViewById(R.id.L8_radioButton4); //第四步:绑定监听器 seekBar.setOnSeekBarChangeListener(new seekBarListener()); ratingBar.setOnRatingBarChangeListener(new ratingBarListener()); radioGroup.setOnCheckedChangeListener(new radioGruopListener()); button1.setOnClickListener(new buttonListener()); button2.setOnClickListener(new buttonListener()); /* progressBar1.setMax(200);//设置最大值为200 progressBar1.setProgress(100);//设置第一进度为100 progressBar1.setSecondaryProgress(150);//设置第二进度为200 int valueProgress1 = progressBar1.getProgress();//得到第一进度的值 int valuesecondProgress1 = progressBar1.getSecondaryProgress();//得到第二进度的值 *//** * 获得进度条是否为模糊进度条 * 返回值:ture - 模糊进度条 * true - 精确进度条 *//* boolean isIndeterminate = progressBar1.isIndeterminate(); progressBar1.incrementProgressBy(10);//相对主进度增加10 progressBar1.incrementSecondaryProgressBy(10);//相对第二进度增加10*/ } //第三步:实现监听器接口 /** * seekBar监听器 */ class seekBarListener implements SeekBar.OnSeekBarChangeListener { @Override /** * seekBar改变出发的监听器 * fromUser : 是否是用户手动操作的 */ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override /** * 用户开始拖拽的时候出发的监听器 */ public void onStartTrackingTouch(SeekBar seekBar) { } @Override /** * 用户停止拖拽的时候出发的监听器 */ public void onStopTrackingTouch(SeekBar seekBar) { System.out.println("SeekBar--> progress = " + seekBar.getProgress()); } } /** * ratioGroup监听器 */ class radioGruopListener implements RadioGroup.OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.L8_radioButton1) { value_radioButton = 1; } else if (checkedId == R.id.L8_radioButton2) { value_radioButton = 2; } else if (checkedId == R.id.L8_radioButton3) { value_radioButton = 3; } else if (checkedId == R.id.L8_radioButton4) { value_radioButton = 4; } } } /** * Button监听器 */ class buttonListener implements View.OnClickListener { @Override public void onClick(View v) { //ProgressBar tempProgressBar; if (v.getId() == R.id.L8_button1) { switch (value_radioButton) { case 1: // progressBar1.incrementProgressBy(10); break; case 2: progressBar2.incrementProgressBy(10); break; case 3: seekBar.incrementProgressBy(10); break; case 4: ratingBar.incrementProgressBy(10); break; } } else if (v.getId() == R.id.L8_button2) { switch (value_radioButton) { case 1: progressBar1.incrementSecondaryProgressBy(10); break; case 2: progressBar2.incrementSecondaryProgressBy(10); break; case 3: seekBar.incrementSecondaryProgressBy(10); break; case 4: ratingBar.incrementSecondaryProgressBy(10); break; } } } } /** * RatingBar监听器 */ class ratingBarListener implements OnRatingBarChangeListener { @Override /** * 这里的formUser可以用来判定是不是用户手动改变 */ public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { System.out.println("RatingBar--> rating = " + rating + " formUser " + fromUser); } } /** * 初始化函数 */ private void L8_AvtivityConfiguration() { progressBar1.setMax(100); progressBar1.setProgress(0); progressBar1.setSecondaryProgress(10); progressBar2.setMax(100); progressBar2.setProgress(0); progressBar2.setSecondaryProgress(10); seekBar.setMax(100); seekBar.setProgress(0); seekBar.setSecondaryProgress(10); ratingBar.setNumStars(5); ratingBar.setStepSize((float) 0.5); radioButton1.setChecked(true); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。