ProgressBar&ProgressDialog

本文详细介绍Android中各种进度条的使用方法,包括普通进度条、老版进度条、自定义进度条及进度对话框的实现。通过示例代码展示了如何设置进度、最大值以及样式等属性。

普通ProgressBar

     <ProgressBar
        android:id="@+id/pb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

普通的progressbar.png
老版的ProgressBar

 <ProgressBar
        android:id="@+id/pb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        style="@android:style/Widget.ProgressBar"
    />

老版的progressbar.png

老版的进度条

     <ProgressBar
        android:id="@+id/pb_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:max="100"
        style="@android:style/Widget.ProgressBar.Horizontal"
    />

这里写图片描述

普通的进度条

  <ProgressBar
        android:id="@+id/pb_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:progress="25"
        android:max="100"
        style="@android:style/Widget.Material.ProgressBar.Horizontal"
        android:secondaryProgress="60"
        />

这里写图片描述

//这里我们设置它的当前进度为60

  <Button
        android:id="@+id/btn_load"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="加载进度"
        android:layout_marginTop="10dp"
        />

//自定义ProgressBar1

 <ProgressBar
        android:id="@+id/pb_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:indeterminateDrawable="@drawable/bg_progress"
        />

这里写图片描述

//自定义ProgressBar2

  <ProgressBar
        android:id="@+id/pb_6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        style="@style/MyProgressBar"
        />

这里写图片描述

//progressdialog1

 <Button
        android:id="@+id/btn_progressdialog1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Progress_Dialog1"
        android:textAllCaps="false"
        android:layout_marginTop="10dp"
        />

这里写图片描述
//progressdialog2

 <Button
        android:id="@+id/btn_progressdialog2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Progress_Dialog2"
        android:textAllCaps="false"
        android:layout_marginTop="10dp"
        />

这里写图片描述

ProgressActivity.java

package net.nyist.lenovo.mytest;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import net.nyist.lenovo.mytest.util.ToastUtil;

public class ProgressActivity extends AppCompatActivity {

    private ProgressBar mPb3;
    private Button mBtnLoading,mBtnProgressDialog1,mBtnProgressDialog2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        mPb3 = (ProgressBar) findViewById(R.id.pb_3);
        mBtnLoading = (Button) findViewById(R.id.btn_load);
        mBtnProgressDialog1 = (Button) findViewById(R.id.btn_progressdialog1);
        mBtnProgressDialog2 = (Button) findViewById(R.id.btn_progressdialog2);
        mBtnProgressDialog1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在加载");
                //如果需要在返回的时候做些什么的,则直接监听它的状态,如果它被关闭,则执行你的操作
                progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        ToastUtil.showMsg(ProgressActivity.this,"cancel....");
                    }
                });
                //希望返回按键失效的话,可以使用setCancelable(false);返回键就不起作用
                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        });
        mBtnProgressDialog2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
                //设置水平进度条
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在下载");
                //设置一个积极地按钮,并可以设置它的监听事件
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    //
                    }
                });
                progressDialog.show();
            }
        });
        //设置pb_3的动态进度条
        /*
        sendEmptyMessage(0)
        在接收消息的时候用到的方法handleMessage(Message msg)
        ,如果if(msg.what ==0)证明是第一个发过来的消息,
        同理如果if(msg.what ==1)证明是第二个发过来的消息。
        这里面数可以随便写。sendEmptyMessage(100),
        那么if(msg.what == 100)就是对应地方发过来的消息*/

        mBtnLoading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0);
            }
        });
    }
    //Handler这个类就是管理某个线程(也可能是进程)的消息队列,以后再详细分析.
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mPb3.getProgress()<100){
                /*
                *postDelayed()文档解释
                * Causes the Runnable r to be added to the message queue, to be run
                * after the specified amount of time elapses.
                * The runnable will be run on the thread to which this handler is attached.
                * 延时delayMillis毫秒 将Runnable插入消息列队,Runnable将在handle绑定的线程中运行。
                * */
                handler.postDelayed(runnable,500);
            }else {
                ToastUtil.showMsg(ProgressActivity.this,"加载完成");
            }
        }
    };
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mPb3.setProgress(mPb3.getProgress()+5);
            handler.sendEmptyMessage(0);
        }
    };
}

自定义1和自定义2还得在drawable中创建一个rootelement为animated-rotate的xml文件

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    //获得图片
    android:drawable="@drawable/icon_progress1"
    //将旋转轴设为图片的中心
    android:pivotX="50%"
    android:pivotY="50%"
    >
</animated-rotate>

自定义2

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/icon_progress"
    android:pivotX="50%"
    android:pivotY="50%"
>
</animated-rotate>

学习内容来自@天哥在奔跑

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值