一 、ProgressDialog
ProgressDialog與ProgressBar在UI中動態(tài)顯示一個加載圖標(biāo)顯示程序運(yùn)行狀態(tài)。
ProgressDialog是繼承自Android.app.ProgressDialog所設(shè)計(jì)的互動對話窗口,使用時,必須新建ProgressDialog對象,在運(yùn)行時會彈出“對話框”作為提醒,此時應(yīng)用程序后臺失去焦點(diǎn)(即此時無法對UI組件進(jìn)行操作),直到進(jìn)程結(jié)束后,才會將控制權(quán)交給應(yīng)用程序,如果在Activity當(dāng)中不希望后臺失焦,又希望提示User有某后臺程序正處于忙碌階段,那么ProgressBar就會派上用場了。
ProgressDialog mProgressDialog = new ProgressDialog(TestProgerssDialogActivity.this);
// 設(shè)置mProgressDialog風(fēng)格
mProgressDialog.setProgress(ProgressDialog.STYLE_SPINNER);//圓形
mProgressDialog.setProgress(ProgressDialog.STYLE_HORIZONTAL);//水平
// 設(shè)置mProgressDialog標(biāo)題
mProgressDialog.setTitle("提示");
// 設(shè)置mProgressDialog提示
mProgressDialog.setMessage("這是一個圓形進(jìn)度條對話框");
// 設(shè)置mProgressDialog進(jìn)度條的圖標(biāo)
mProgressDialog.setIcon(R.drawable.flag);
// 設(shè)置mProgressDialog的進(jìn)度條是否不明確
//不滾動時,當(dāng)前值在最小和最大值之間移動,一般在進(jìn)行一些無法確定操作時間的任務(wù)時作為提示,明確時就是根據(jù)你的進(jìn)度可以設(shè)置現(xiàn)在的進(jìn)度值
mProgressDialog.setIndeterminate(false);
//mProgressDialog.setProgress(m_count++);
// 是否可以按回退鍵取消
mProgressDialog.setCancelable(true);
// 設(shè)置mProgressDialog的一個Button
mProgressDialog.setButton("確定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
// 顯示mProgressDialogmProgressDialog.show();
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
mProgressDialog.hide();
webview.stopLoading();
}
});
當(dāng)有ProgressDialog時,點(diǎn)擊back時,會首先取消ProgressDialog ,以上代碼可以監(jiān)聽進(jìn)度條被取消事件(也就是點(diǎn)擊Back鍵取消ProgressDialog),此時可以在這里作一些取消后臺操作的處理。
二、ProgressBar
XML重要屬性
android:progressBarStyle:默認(rèn)進(jìn)度條樣式
android:progressBarStyleHorizontal:水平樣式重要方法
getMax():返回這個進(jìn)度條的范圍的上限
getProgress():返回進(jìn)度
getSecondaryProgress():返回次要進(jìn)度
incrementProgressBy(int diff):指定增加的進(jìn)度
isIndeterminate():指示進(jìn)度條是否在不確定模式下
setIndeterminate(boolean indeterminate):設(shè)置不確定模式下
setVisibility(int v):設(shè)置該進(jìn)度條是否可視
重要事件
onSizeChanged(int w, int h, int oldw, int oldh):當(dāng)進(jìn)度值改變時引發(fā)此事件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to blog" />
<ProgressBar
android:id="@+id/rectangleProgressBar"
style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/circleProgressBar"
style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button android:id="@+id/button"
android:text="Show ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>