1. 程式人生 > >Android開發實戰之——ProgressDialog的使用(一)

Android開發實戰之——ProgressDialog的使用(一)

ProgressDialog的使用

Android原生的ProgressDialog分為兩類
1. 一類是進度條不明確的
2. 另一類是進度條明確的
展示的形式有圓形和水平進度條
注意:對於不明確的進度條才可以設定Indeterminate為true,對於明確的進度條,需要設定為false。
佈局檔案XML如下所示:
主要是通過按鈕觸發進度條

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop
="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.android.progressbar.MainActivity">
<Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="進度條1"/> <Button
android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="進度條2"/>
<Button android:id="@+id/btn3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="進度條3"/> </LinearLayout>

對於進度條1,採用的是預設的圓形佈局,通過呼叫靜態的方法即可實現(是屬於進度條的進度值不明確的情況,滾動條自動在最大值和最小值之間來回滾動,形成一個動畫的效果,這個只是告訴別人,“我正在工作”,但不能提示工作進度到了哪個階段,主要是進行一些無法確定操作時間的任務時作為提示)
對於進度條2,採用的是水平佈局,也是屬於進度值不明確的情況。
對於進度條3,採用的是水平佈局,模擬的是進度值明確的情況。
對於進度條
MainActivity如下所示:

package com.android.progressbar;

import android.app.ProgressDialog;
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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public Button btn1,btn2,btn3;//分別表示三個不同的進度條
    final static int MAX_PROGRESS=100;
    private int[]data=new int[50];
    public int progressStatus=0;
    public int hasData=0;
    public ProgressDialog pd1,pd2;//定義兩個進度條對話方塊
    //定義一個Handler用於處理更新進度條
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //表明是由該程式傳送的
            if(msg.what==0x123){
                pd2.setProgress(progressStatus);

            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1= (Button) findViewById(R.id.btn1);
        btn2= (Button) findViewById(R.id.btn2);
        btn3= (Button) findViewById(R.id.btn3);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn1:
                showSpinner(v);
                break;
            case R.id.btn2:
                showProgress(v);

                break;
            case R.id.btn3:
                showIndeterminate(v);
                break;
        }

    }
    public void showSpinner(View source){
        //呼叫靜態方法顯示環形進度條
        ProgressDialog.show(this,"任務執行中","任務執行中請稍等",false,true);
    }
    public void showIndeterminate(View source){
        pd1=new ProgressDialog(MainActivity.this);
        pd1.setTitle("任務正在執行中");//設定標題
        pd1.setMessage("任務正在執行中,敬請等待...");//設定訊息
        pd1.setCancelable(true);//設定進度條是不是可以取消
        pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//設定對話方塊的進度條的風格
        pd1.setIndeterminate(true);//設定對話方塊進度條是否顯示進度
        pd1.show();//顯示進度條
    }

    public void showProgress(View source){
        //將進度條的完成進度重設為0
        progressStatus=0;
        //重新開始填充陣列
        hasData=0;
        pd2=new ProgressDialog(MainActivity.this);
        pd2.setMax(MAX_PROGRESS);
        //設定對話方塊的標題
        pd2.setTitle("任務完成百分比");
        //設定對話方塊顯示的內容
        pd2.setMessage("耗時任務的完成百分比");
        //設定對話方塊不能用“取消”按鈕關閉
        pd2.setCancelable(false);
        //設定對話方塊的進度條風格
        pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //設定對話方塊的進度條是否顯示進度
        pd2.setIndeterminate(false);
        pd2.show();
        new Thread(){
            @Override
            public void run() {
                while(progressStatus<MAX_PROGRESS){
                    //獲取耗時操作的完成百分比
                    progressStatus=MAX_PROGRESS*doWork()/data.length+5;
                    //傳送訊息到handler
                    handler.sendEmptyMessage(0x123);
                }
                if(progressStatus>=MAX_PROGRESS){
                    //關閉對話方塊
                    pd2.dismiss();
                }
            }
        }.start();
    }
    //模擬一個耗時操作
    public int doWork(){
        data[hasData++]=(int)(Math.random()*100);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hasData;
    }
}