【安卓学习笔记】Android Studio第9课——进度条ProgressBar、SeekBar和RatingBar
16lz
2021-01-23
总体上Android默认常用的进度条分为四种:
1、垂直风格的圈状ProgressBar
这种转圈形式的进度条可以一般用作模糊指示,换句话说这个进度条无法体现出来当前的精确进度,只能傻傻的转圈。
在设置上有:
style="?android:progressBarStyleLarge"//大style="?android:progressBarStyleSmall"//小
这两个属性比较常见,其次还有inverse(反转),效果好像有跟没有一样 2、水平风格的条状ProgressBar
条状的进度条就可以设置和体现出精确的进度。
在属性里面需要设置成水平方向:
style="?android:progressBarStyleHorizontal"
2.1常用到的参数有max、progress、secondaryProgress,分别代表了进度条的最大值,当前的进度值和第二进度值。这里解释下secondaryProgress,这里打个比方:假如一个任务需要分为步骤依次的完成,第一个步骤是建立在第二个步骤基础上的,那么这种情况就很适合用progress表示第一个步骤完成的进度,用secondaryProgress表示第二步骤完成的进度。 2.2常用到方法有
progressBar1.setMax(200);//设置最大值为200progressBar1.setProgress(100);//设置第一进度为100progressBar1.setSecondaryProgress(150);//设置第二进度为150/** * 获得进度条是否为模糊进度条 * 返回值:ture - 模糊进度条 * true - 精确进度条 */boolean isIndeterminate = progressBar1.isIndeterminate();progressBar1.incrementProgressBy(10);//相对主进度增加10progressBar1.incrementSecondaryProgressBy(10);//相对第二进度增加10
3、可拖拽的SeekBar SeekBar是ProgressBar的子类,跟ProgressBar的水平风格很相似,也有max、progress和secondaryProgress三个重要的参数
常用的设置方法如下:
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()); }
所以根据软件和应用的需求可以自己定义需要用到哪一个。 4、评分用的RatingBar
ratingBar.setNumStars(5); ratingBar.setStepSize((float) 0.5);
也有对应的监听器OnRatingBarChangeListener,方法都很相似,就不贴单独贴代码。 程序只是用来测试用,肯定漏洞百出,下面给出测试的Activity并贴上全部代码:
、
XML:
<?xml version="1.0" encoding="utf-8"?>
Activity.java 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); }}
By Urien 2018年6月3日 17:57:28 更多相关文章
- Android 进度条功能实现
- android 之进度条--------progressbar
- 关于android读取用户号码,手机串号,SIM卡序列号
- android 自定义进度条颜色
- android卸载应用的进度条
- android下保存用户账号密码和读取账号密码