【邊做項目邊學Android】知識點:Android控件系列之ProgressDialog與ProgressBar
來源:程序員人生 發布時間:2014-11-10 08:53:28 閱讀次數:2660次
ProgressDialog
ProgressDialog與ProgressBar在UI中動態顯示1個加載圖標顯示程序運行狀態。
ProgressDialog是繼承自Android.app.AlertDialog所設計的互動對話窗口,使用時,必須新建ProgressDialog對象,在運行時會彈出“對話框”作為提示,此時利用程序后臺失去焦點(即此時沒法對UI組件進行操作),直到進程結束后,才會將控制權交給利用程序,如果在Activity當中不希望后臺失焦,又希望提示User有某后臺程序正處于繁忙階段,那末ProgressBar就會派上用處了。
ProgressDialog pd= new ProgressDialog(TestProgerssDialogActivity.this);
// 設置pd風格
pd.setProgress(ProgressDialog.STYLE_SPINNER);//圓形
pd.setProgress(ProgressDialog.STYLE_HORIZONTAL);//水平
// 設置pd標題
pd.setTitle("提示");
// 設置pd提示
pd.setMessage("這是1個圓形進度條對話框");
// 設置pd進度條的圖標
pd.setIcon(R.drawable.flag);
// 設置pd的進度條是不是不明確
//不轉動時,當前值在最小和最大值之間移動,1般在進行1些沒法肯定操作時間的任務時作為提示,明確時就是根據你的進度可以設置現在的進度值
pd.setIndeterminate(false);
//pd.setProgress(m_count++);
// 是不是可以按回退鍵取消
pd.setCancelable(true);
// 設置pd的1個Button
pd.setButton("肯定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
// 顯示pd.show();
當有ProgressDialog時,點擊back時,會首先取消ProgressDialog ,以上代碼可以監聽進度條被取消事件(也就是點擊Back鍵取消ProgressDialog),此時可以在這里作1些取消后臺操作的處理。
ProgressBar
XML重要屬性
android:progressBarStyle:默許進度條樣式
android:progressBarStyleHorizontal:水平樣式
重要方法
getMax():返回這個進度條的范圍的上限
getProgress():返回進度
getSecondaryProgress():返回次要進度
incrementProgressBy(int diff):指定增加的進度
isIndeterminate():唆使進度條是不是在不肯定模式下
setIndeterminate(boolean indeterminate):設置不肯定模式下
setVisibility(int v):設置該進度條是不是可視
重要事件
onSizeChanged(int w, int h, int oldw, int oldh):當進度值改變時引發此事件
<?xml version="1.0" encoding="utf⑻"?>
<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>
參考:
http://www.jb51.net/article/38532.htm
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈