本文實例講述了Android開發(fā)之ProgressDialog進度對話框用法。分享給大家供大家參考,具體如下:
使用方法:
如果只是普通進度對話框 | 直接調(diào)用ProgressDialog靜態(tài)方法 show() 即可 |
創(chuàng)建ProgressDialog | 調(diào)用方法進行具體設(shè)置 |
setIndeterminate(false); |
設(shè)置進度條是否顯示進度 |
setMax(MAX_PROGRESS); |
設(shè)置進度條的最大值 |
setMessage("..."); |
設(shè)置對話框執(zhí)行內(nèi)容 |
setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) |
設(shè)置對話框進度條風格 |
setProgress() |
設(shè)置 進度條內(nèi)的進度值(當前) |
效果:
代碼實現(xiàn):
主要是兩種類型
1.帶耗時操作 showIndeterminate
2.不帶耗時操作 showProgress
具體實現(xiàn):
public class MainActivity extends Activity {
final static int MAX_PROGRESS = 100;
//虛擬 填充長度為100的數(shù)組
private int[] data = new int[50];
//記錄進度對話框完成百分比
int progressStatus = 0;
int hasDta = 0;
ProgressDialog progressDialog01,progressDialog02;
private Button buttonDate;
private Button buttonTime;
//創(chuàng)建一個負責更新進度的Handler
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
//表明消息是本程序發(fā)送的
if (msg.what == 0x111){
progressDialog02.setProgress(progressStatus);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonDate = (Button) findViewById(R.id.dataBn);
buttonTime = (Button) findViewById(R.id.timeBn);
iniClick();//Binding the listeners for you program
}
public void showSpinner(View source){
//用靜態(tài)方法顯示環(huán)形進度條
ProgressDialog.show(this,"任務(wù)執(zhí)行中","熱內(nèi)執(zhí)行中 請等待...",false,true);
}
public void showIndeterminate(View source){
progressDialog01 = new ProgressDialog(MainActivity.this);
//設(shè)置對話框標題
progressDialog01.setTitle("任務(wù)正在執(zhí)行中");
//設(shè)置對話框執(zhí)行內(nèi)容
progressDialog01.setMessage("任務(wù)正在執(zhí)行中敬請等待~~~");
//設(shè)置對話框“取消” 按鈕關(guān)閉
progressDialog01.setCancelable(true);
//設(shè)置對話框進度條風格
progressDialog01.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設(shè)置進度條是否顯示進度
progressDialog01.setIndeterminate(true);
progressDialog01.show();
}
public void showProgress(View source){
//將進度條完成重設(shè)為0
progressStatus = 0;
//重新開始填充數(shù)組
hasDta = 0;
progressDialog02 = new ProgressDialog(MainActivity.this);
progressDialog02.setMax(MAX_PROGRESS);
//設(shè)置對話框標題
progressDialog02.setTitle("任務(wù)正在執(zhí)行中");
//設(shè)置對話框執(zhí)行內(nèi)容
progressDialog02.setMessage("任務(wù)正在執(zhí)行中敬請等待~~~");
//設(shè)置對話框“取消” 按鈕關(guān)閉
progressDialog02.setCancelable(false);
//設(shè)置對話框進度條風格
progressDialog02.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設(shè)置進度條是否顯示進度
progressDialog02.setIndeterminate(false);
progressDialog02.show();
new Thread(){//模擬耗時操作
@Override
public void run() {
while (progressStatus < MAX_PROGRESS){
//bar1獲取完成工作的百分比
progressStatus += (int) (Math.random()*15);
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
//更新ProgressBar
mHandler.sendEmptyMessage(0x111);
}
//任務(wù)完成進度條關(guān)閉
progressDialog02.dismiss();
}
}.start();
}
public void iniClick(){
//set listener for your Date button
buttonDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showIndeterminate(MainActivity.this.getWindow().getDecorView());
}
});
//set listener for your Time button
buttonTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showProgress(MainActivity.this.getWindow().getDecorView());
}
});
}
}
以下是布局文件:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/idtatabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="@+id/dataBn"
android:text="一般進度條"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:id="@+id/timeBn"
android:text="帶耗時進度條"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!