使用ProgressBar显示进度条

此文,仅做为个人学习Android,记录成长以及方便复习!

首先是设置UI界面

1.activity_main.xml

每个按钮都添加了 android:onClick="onclick" 方便使用监听方法,不用实例化按钮

ProgressBar的几个常用参数

style="?android:attr/progressBarStyleHorizontal"   //设置显示样式,此为显示进度的样式
android:progress="50"                //第一进度
android:secondaryProgress="80"        //第二进度
android:max="100"                    //最大刻度

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:progress="50"
        android:secondaryProgress="80"
        android:max="100"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加10%"
        android:onClick="onclick"
        android:layout_below="@+id/progressBar"/>
    <Button
        android:id="@+id/reduction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="减加10%"
        android:onClick="onclick"
        android:layout_below="@+id/add"/>
    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="重置"
        android:onClick="onclick"
        android:layout_below="@+id/reduction"/>

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:textSize="20sp"
        android:layout_below="@+id/reset"/>
</RelativeLayout>

接下来就是Activity

MainActivity.java

package com.rui.progressbardome;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ProgressBar progressBar;//创建ProgressBar
    private TextView tv1;//创建TextView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//调用init方法
    }

    public void init(){
        progressBar = findViewById(R.id.progressBar);
        tv1 = findViewById(R.id.tv1);
        int max = progressBar.getMax();//获取进度最大值
        int progress= progressBar.getProgress();//获取第一进度
        int  sdProgress= progressBar.getSecondaryProgress();//获取第二进度
        //百分比是先把除数的整型转换成浮点型,相除的结果是一个0~1之间的小数
        // 再乘以100得到0~100之间的小数,最后把这个小数转换成整数。
        // 其实不用那么麻烦,改成 100*progress/max 和 100*sdProgress/max 就可以了:
        tv1.setText("第一进度百分比:"+(int)((progress/(float)max)*100)+"% 第二进度百分百:"+(int)((sdProgress/(float)max)*100));
    }
    //按钮监听事件
    public void onclick(View view){
        switch(view.getId()){
            case R.id.add:
                //第一进度增加10
                progressBar.incrementProgressBy(10);
                //第二进度增加10
                progressBar.incrementSecondaryProgressBy(10);
                break;
            case R.id.reduction:
                //第一进度减少10
                progressBar.incrementProgressBy(-10);
                //第二进度减少10
                progressBar.incrementSecondaryProgressBy(-10);
                break;
            case R.id.reset:
                //设置第一进度
                progressBar.setProgress(50);
                //设置第二进度
                progressBar.setSecondaryProgress(80);
                break;
        }
        //重新获取进度并在tv1显示!
        tv1.setText("第一进度百分比:"+progressBar.getProgress()+"% 第二进度百分百:"+progressBar.getSecondaryProgress());
    }
}

运行后,单击增加按钮2次 效果如下


单击减少按钮1次 效果如下


点击重置按钮如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值