當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > 掌握Android對(duì)話框
一.目標(biāo)
1. 使用ProgressDialog.show()方式創(chuàng)建對(duì)話框
2. 使用new PorgressDialog(context)方式創(chuàng)建對(duì)話框
二. 過程
1. 使用ProgressDialog.show(....)方式創(chuàng)建對(duì)話框
(1)方式1
// 方式1 使用靜態(tài)方式創(chuàng)建并顯示,這種進(jìn)度條只能是圓形條,這里后一個(gè)參數(shù)設(shè)置是否進(jìn)度條是可以取消的 ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陸中", false, true);
(2)方式2
DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { Toast.makeText(MainActivity.this, "進(jìn)度條被取消", Toast.LENGTH_LONG).show(); } }; // cancelListener用于監(jiān)聽進(jìn)度條被取消 ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陸中", fasle, true, cancelListener);
2. 創(chuàng)建new PorgressDialog(context)方式創(chuàng)建對(duì)話框
(方式一)創(chuàng)建圓形進(jìn)度條對(duì)話框
ProgressDialog dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 設(shè)置圓形轉(zhuǎn)動(dòng)進(jìn)度條 dialog.setMessage("正在下載..."); dialog.show();
(方式二)創(chuàng)建條形進(jìn)度條對(duì)話框
final ProgressDialog dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 設(shè)置圓形轉(zhuǎn)動(dòng)進(jìn)度條 dialog.setMax(100);//設(shè)置總進(jìn)度 dialog.setProgress(0);//設(shè)置當(dāng)前進(jìn)度0 // dialog.setSecondaryProgress(0);//設(shè)置二級(jí)進(jìn)度 // dialog.incrementProgressBy(0);//設(shè)置進(jìn)度在以前基礎(chǔ)上追加多少 設(shè)置一級(jí)進(jìn)度 // dialog.incrementSecondaryProgressBy(0);//設(shè)置二級(jí)進(jìn)度 dialog.setMessage("正在下載..."); dialog.setCancelable(false); dialog.show();
示例:
/layout/activity_main.xml