【进度显示】progressbar进度条的使用

ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性

ProgressBar的分类:有无进度条的分别

ProgressBar在XML设置
大环形progressBar:style=”?android:attr/progressBarStyleLarge”
中环形progressBar:style=不用设置
小环形progressBar: style=”?android:attr/progressBarStyleSmall”
水平的progressBar: style=”?android:attr/progressBarStyleHorizontal”

XML:ProgressBar属性

android:max="100"--最大显示进度  
android:Progress="100"--第一显示进度 
android:secondProgress="80"--第二显示进度   
android:indeterminate="true"--设置是否精确显示
true表示不精确显示、false表示精确

自定义ProgressBar样式

style="@android:style/Widget.ProgressBar.Horizontal"//安卓自带的一个风格 水平横向进度条

android:progressDrawable="@drawable/progress_bar"//设置progressDrawable覆盖自带风格
android:radius="5dip" 设置圆角
android:startColor="#ff9d9e9d" 起始颜色
android:centerColor="#ff5a5d5a" 中间颜色
android:endColor="#ff747674" 终止颜色
android:angle="270"

ProgressBar的关键方法

setProgress(int)设置第一进度
setSecondaryProgress(int)设置第二进度 
getProgress()获取第一进度 
getSecondaryProgress()获取第二进度 
incrementProgressBy(int)增加或减少第一进度 
incrementSecondaryProgressBy(int)增加或减少第二进度 
getMax()获取最大进度

ProgressDiglog的基础使用

proDialog=new ProgressDialog(MainActivity.this);//新建progressDialog对象
proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置显示风格
proDialog.setTitle("");//设置标题
proDialog.setMessary("");//对话框里面的文字
proDialog.setIcon(R.drawable.ic_launch);//设置图标
proDialog.setMax(100);//进度条的最大值
proDialog.incrementProgressBy(40);//设定初始化已经增长了的进度
proDialog.setIndeterminate(false);//设定明确显示进度
proDialog.setButton(whichButton,text,listener);//连接监听器
proDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定",new //设定一个确认按钮

proDialog.setButton(DialogInterface.BUTTON_POSITIVE, 
"确定", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int witch) {
        Toast.makeText(MainActivity.this
            ,"欢迎大家支持慕课网",Toast.LENGTH_SHORT).show();
    }
});
proDialog.setCancelable(true);//设置为false后返回键不能退出,默认为true
proDialog.show();//显示对话框

源代码↓↓↓↓↓

package com.example.progressbar;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/*
 * progressBar是进度条组件,通常用于向用户展示某个耗时操作完成进度
 * 而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性。
 */

public class MainActivity extends Activity implements OnClickListener {

    private ProgressBar progress;
    private Button add;
    private Button reduce;
    private Button reset;
    private TextView text;
    private ProgressDialog prodialog;
    private Button show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 插入:启用窗口特征,启用带进度和不带进度的进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        setContentView(R.layout.activity_main);

        // 显示两种进度条:true就显示,false就消失
        setProgressBarVisibility(true);// 可见刻度
        setProgressBarIndeterminateVisibility(false);// 不可见
        // Max=10000,最后一个值是9999
        setProgress(9999);

        init();

    }

    private void init() {
        progress = (ProgressBar) findViewById(R.id.horiz);
        add = (Button) findViewById(R.id.add);
        reduce = (Button) findViewById(R.id.reduce);
        reset = (Button) findViewById(R.id.reset);
        text = (TextView) findViewById(R.id.text);
        show = (Button) findViewById(R.id.show);

        // getPrgress()获取第一、第二、最大进度条的进度
        int first = progress.getProgress();
        int second = progress.getSecondaryProgress();
        int max = progress.getMax();
        // 显示的文本百分比进度
        text.setText("第一进度百分比" + (int) (first / (double) max * 100) + "% "
                + "第二进度百分比" + (int) (second / (double) max * 100) + "% ");

        // 添加各个进度器的监听器
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        reset.setOnClickListener(this);
        show.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.add: {
            // 增加第一进度第二进度的10个刻度
            progress.incrementProgressBy(10);
            progress.incrementSecondaryProgressBy(10);
            break;
        }

        case R.id.reduce: {
            // 减少第一进度第二进度的10个刻度
            progress.incrementProgressBy(-10);
            progress.incrementSecondaryProgressBy(-10);
            break;
        }
        case R.id.reset: {
            // 重置到原始数值
            progress.setProgress(50);
            progress.setSecondaryProgress(80);
            break;
        }
        case R.id.show: {

            /**
             * 页面显示风格
             */
            // 新建ProgressDialog对象
            prodialog = new ProgressDialog(MainActivity.this);
            // 设置显示风格
            prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            // 设置标题
            prodialog.setTitle("帅帅嗒先锋");
            // 设置对话框里面的文字信息
            prodialog.setMessage("帅帅嗒先锋真TM帅!");
            // 设置图标
            prodialog.setIcon(R.drawable.ic_launcher);

            /**
             * 设定关于ProgressBar的一些属性
             */
            // 最大进度为100
            prodialog.setMax(100);
            // 设定初始化现有的进度
            prodialog.incrementProgressBy(50);
            // 进度条是明确显示进度的
            prodialog.setIndeterminate(false);

            /**
             * 设定一个确定按钮
             */
            prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "欢迎大家支持郑先锋",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
            // 是否可以通过返回按钮退出对话框
            prodialog.setCancelable(true);

            // 显示progressDialog
            prodialog.show();

            break;
        }
        }
        text.setText("第一进度百分比"
                + (int) (progress.getProgress() / (double) progress.getMax() * 100)
                + "% "
                + "第二进度百分比"
                + (int) (progress.getSecondaryProgress()
                        / (double) progress.getMax() * 100) + "% ");
    }
}

布局文件:↓↓↓↓↓

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- ↓↓↓↓↓↓↓按住Ctrl然后点击style 查看或者修改进度条的调色和配置↓↓↓↓↓↓ -->
    <ProgressBar
        android:secondaryProgress="80"
        android:progress="50"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"
        android:id="@+id/horiz"
        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="@string/add" />

    <Button
        android:id="@+id/reduce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reduce" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show" />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值