android学习之四·使用android进度条控件progressbar



/bywinkey 整理时间:2014122422:58:46

  1. 概述
    在处理一个比较耗时的事件时,往往界面上都需要有一个等待过程,无论是window的启动或者是QQ的登陆,以及文件的下载等等。通常进度条就会在上述场景中出现,特别是文件的下载。利用前台显示进度条使得用户不会感觉程序在后台处理中失去相应。反而提高了用户界面的友好性。本节实现一个 模拟文件下载的进度条

  2. 预备知识:
    ProgressBar
    组件是android系统内置的组件,其有多种风格, 如下表所示

属性值

说明

Horizontal

水平进度条

Small

小进度条

Large

大进度条

Inverse

不断跳跃、旋转动画进度条

Large.Inverse

不断跳跃、旋转动画的大进度条

Small.Inverse

不断跳跃、旋转动画的小进度条

在上述风格中仅有 Horizontal 支持进度递增 ProgressBar 常用常用属性及使用说明

属性

说明

max

最大值

Progress

第一进度值

SecondaryProgress

第二进度值

Visibility

是否显示,默认显示

上述属性可以通过如下方法进行设置和获取

方法

说明

int getMax()

获取最大值

Int getProgress()

获取当前第一进度

int getSecondaryProgress

获取当前第二进度

void setMax(int max)

设置最大值

void setProgress(int progress)

设置第一进度值

void setSecondaryProgress(int secondaryProgress)

设置第二进度值

void setVisibility(int v)

设置是否显示,默认显示

在此案例中要使用到Handler Message 此处不做详细介绍

  1. 步骤

  1. 创建Model

  2. Layout文件中添加 ProgressBar组件

    代码如下:

    <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ProgressBar">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/linear1"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/sizeView"
                    android:text="文件大小:"
                    android:textSize="20sp"
                    />
                <EditText
                    android:id="@+id/inputEdit"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="3dp"
                    android:inputType="number"
                    android:textSize="20sp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:text="MB"
                    android:textSize="20sp"
                />
                </LinearLayout>
            <LinearLayout
                android:id="@+id/linear2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                >
                <ProgressBar
                    android:id="@+id/progressbar1"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="255dp"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dp"
                    android:visibility="visible"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dp"
                    android:text="进度"
                    android:textSize="12sp"
                    android:id="@+id/persentView"
                    />
                </LinearLayout>
            <Button
                android:id="@+id/downBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下载"
                android:textSize="20sp"
                />
            </LinearLayout>
    
    </RelativeLayout>

3.activity中添加如下代码

private android.widget.ProgressBar pb;
    private EditText inputEd;//文件大小录入框
    private Button downbtn;//开始下载按钮
    private TextView showView;//
    private int progressValue = 0;//设定初始值
    private String s;
    private int size;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar);
        pb = (android.widget.ProgressBar) findViewById(R.id.progressbar1);
        inputEd = (EditText) findViewById(R.id.inputEdit);
        showView = (TextView) findViewById(R.id.persentView);
        downbtn = (Button) findViewById(R.id.downBtn);
        downbtn.setOnClickListener(new BtnListener());
    }
    //内部类 按钮按下触发的事件
    class BtnListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            s = inputEd.getText().toString();//获取文本框中输入的值
            size = Integer.parseInt(s);//convert为数值
            pb.setMax(size);//设定进度条最大数量
            Toast.makeText(ProgressBar.this,"43",Toast.LENGTH_SHORT).show();
            Thread thread = new Thread(myRun);//调用ui线程
            thread.start();
        }

    }
    Runnable myRun = new Runnable() {
        @Override
        public void run() {
            while(true){
                //获取主线程Handler的Message
                Message msg = handler.obtainMessage();
                //进度条值作为消息的参数进行封装,进度+ 1
                msg.arg1 = progressValue ++;
                //将此消息发送给主线程 handel
                handler.sendMessage(msg);
                if(progressValue > size){//如果刺客的progressvalue 大于 文件值 即可退出
                    break;
                }
                try {
                    Thread.sleep(100); //休眠一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    android.os.Handler handler = new android.os.Handler(new android.os.Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            pb.setProgress(msg.arg1);//接收另一个线程的Message,参数arg1代表了进度
            showView.setText((int)(msg.arg1 * 1.0 / size * 100) + "%");//设置显示的进度
            pb.setSecondaryProgress(msg.arg1 + 10);//设置第二进度
            return false;
        }
});


启动程序之后在文本框中输入一个文件的大小点击下载,模拟文件下载进度


运行结果:



    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值