Android Studio——进度条ProgressBar、SeekBar、RatingBar

总体上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);//设置最大值为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是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也是ProgressBar的子类,这个风格常用来做评分或者评等级用。常用到参数有星星个数、步进。用法如下:
     

   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

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);
 
    }
 
}

 

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道了。下面是您所需要的代码: XML文件: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher_background" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" android:layout_alignParentBottom="true"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_weight="1" android:max="100" android:progress="0" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:numStars="5" android:stepSize="1.0" /> </LinearLayout> </LinearLayout> ``` Java文件: ``` public class MainActivity extends AppCompatActivity { private ImageView imageView; private SeekBar seekBar; private ProgressBar progressBar; private RatingBar ratingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); seekBar = findViewById(R.id.seekBar); progressBar = findViewById(R.id.progressBar); ratingBar = findViewById(R.id.ratingBar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { imageView.setAlpha((float) progress / 255); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast.makeText(MainActivity.this, "Rating: " + rating, Toast.LENGTH_SHORT).show(); } }); } } ``` 运行效果: ![image](https://user-images.githubusercontent.com/38335321/122733356-37ccff00-d2a8-11eb-9a0f-3c9843dc5d5a.png) 希望这可以帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值