ProgressBar、ProgessDialog用法解析


一、ProgressBar

1. 常用類型

1.1 不確定式圓形進度條
style="@android:style/Widget.Holo.Light.ProgressBar"
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"
...

  沒有顯示進度,可作為過場動畫。有大、中、小三種大小,默認為中。

1.2 條形進度條
style="@android:style/Widget.ProgressBar.Horizontal"
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
...

  帶有顯示進度。

1.3 標題欄不確定式進度條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);

  在標題欄右側顯示的無顯示進度的圓形進度條。

1.4 標題欄條形進度條
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);

  在標題欄頂部顯示的條形進度條,可通過setProgess(int)設置當前進度,最大值為10000。

2. 常用控件屬性

<!--最大顯示進度-->
android:max
<!--第一顯示進度-->
android:progress
<!--第二顯示進度-->
android:secondaryProgress
<!--置是否精確顯示;true為不精確,false為精確-->
android:indeterminate
<!--加載自定義樣式-->
android:progressDrawable

3. 自定義樣式

  通過控件的android:progressDrawable屬性引用自定義的drawable文件實現。一般需定義三個內容:背景、第一進度、第二進度。

  范例:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!--背景樣式-->
    <item android:id="@android:id/background">
        <shape>
            <!--圓角-->
            <corners android:radius="10dip" />
            <!--填充色-->
            <solid android:color="#dddddd" />
        </shape>
    </item>

    <!--第二進度樣式-->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="10dip" />
                <solid android:color="#78bb78" />
            </shape>
        </clip>
    </item>

    <!--第一進度樣式-->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dip" />
                <solid android:color="#55bb55" />
            </shape>
        </clip>
    </item>

</layer-list>

  貼張效果圖:

ProgressBar自定義樣式

4. 關鍵方法

//設置第一進度
setProgress(int)
//設置第二進度
setSecondaryProgress(int)
//獲取第一進度
getProgress()
//獲取第二進度
getSecondaryProgress()
//增加或減少第一進度
incrementProgressBy(int)
//增加或減少第二進度
incrementSecondaryProgressBy(int)
//獲取進度最大值
getMax()

5. 范例

布局比較簡單,線性布局,豎直排列,這里就不貼代碼了,直接貼張圖:

ProgressBar演示范例

Java:

public class ProgessBarActivity extends Activity implements View.OnClickListener{

    private ProgressBar progressBar;
    private TextView text;
    private Button addFirst;
    private Button addSecond;
    private Button subFirst;
    private Button subSecond;
    private Button reset;
    private int first;
    private int second;
    private int max;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progess_bar);

        init();

    }

    private void init() {
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        text = (TextView) findViewById(R.id.text);
        addFirst = (Button) findViewById(R.id.add_first);
        subFirst = (Button) findViewById(R.id.sub_first);
        addSecond = (Button) findViewById(R.id.add_second);
        subSecond = (Button) findViewById(R.id.sub_second);
        reset = (Button) findViewById(R.id.reset);

        //獲取第一、第二、最大進度
        first = progressBar.getProgress();
        second = progressBar.getSecondaryProgress();
        max = progressBar.getMax();

        addFirst.setOnClickListener(this);
        addSecond.setOnClickListener(this);
        subFirst.setOnClickListener(this);
        subSecond.setOnClickListener(this);
        reset.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_first:
                //第一進度加10
                progressBar.incrementProgressBy(10);
                break;
            case R.id.add_second:
                //第二進度加10
                progressBar.incrementSecondaryProgressBy(10);
                break;
            case R.id.sub_first:
                progressBar.incrementProgressBy(-10);
                break;
            case R.id.sub_second:
                progressBar.incrementSecondaryProgressBy(-10);
                break;
            case R.id.reset:
                //重置為初始數值
                progressBar.setProgress(30);
                progressBar.setSecondaryProgress(60);
                break;
        }
        //更新文本內容
        text.setText("第一進度為" + (int) (1.0*first/max*100) + "%,第二進度為" + (int) (1.0*second/max*100) + "%");
    }
}

二、ProgressDialog

1. 構造函數

ProgressDialog(Context context)
ProgressDialog(Context context, int theme)//theme為對話框樣式

2. 關鍵方法

//設置進度條樣式
setProgressStyle(int style)
//設置對話框標題
setTitle(String title)
//設置對話框本文信息
setMessage(CharSequence message)
//設置對話框圖標
setIcon(Drawable d)
//設置按鈕,whichButton為按鈕類型,text為按鈕名稱,listener為監聽器
setButton(int whichButton, CharSequence text, OnClickListener listener)
//顯示對話框
show()

  此外,除了這幾個方法,ProgressDialog也可使用上面ProgressBar中介紹的方法。

3. 范例

public class ProgressDialogActivity extends Activity {

    private ProgressDialog proDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_dialog);

        findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //新建對話框
                proDialog = new ProgressDialog(ProgressDialogActivity.this);
                //設置進度條樣式
                proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //設置對話框標題
                proDialog.setTitle("初識ProgressDialog");
                //設置提示對話框文本
                proDialog.setMessage("好好學習,天天向上!");
                //設置對話框顯示圖標
                proDialog.setIcon(R.drawable.ic_launcher);
                //設置進度條最大進度,默認為10000
                proDialog.setMax(100);
                //設置初始第一進度
                proDialog.incrementProgressBy(30);
                //設定取消按鈕
                proDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                //顯示對話框
                proDialog.show();
            }
        });
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM