ProgressDialog使用总结
生活随笔
收集整理的這篇文章主要介紹了
ProgressDialog使用总结
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ProgressDialog的使用?
ProgressDialog 繼承自AlertDialog,AlertDialog繼承自Dialog,實(shí)現(xiàn)DialogInterface接口。
ProgressDialog的創(chuàng)建方式有兩種,一種是new Dialog ,一種是調(diào)用Dialog的靜態(tài)方法Dialog.show()。
? ? ??
// 方式一:new Dialogfinal ProgressDialog dialog = new ProgressDialog(this);dialog.show();// 方式二:使用靜態(tài)方式創(chuàng)建并顯示,這樣的進(jìn)度條僅僅能是圓形條,設(shè)置title和Message提示內(nèi)容ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陸中");
// 方式三 使用靜態(tài)方式創(chuàng)建并顯示,這樣的進(jìn)度條僅僅能是圓形條,這里最后一個(gè)參數(shù)boolean indeterminate設(shè)置是否是不明白的狀態(tài)ProgressDialog dialog3 = ProgressDialog.show(this, "提示", "正在登陸中", false);
// 方式四 使用靜態(tài)方式創(chuàng)建并顯示,這樣的進(jìn)度條僅僅能是圓形條,這里最后一個(gè)參數(shù)boolean cancelable 設(shè)置是否進(jìn)度條是能夠取消的ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陸中",false, true);
方式五中須要一個(gè)cancelListener,代碼例如以下;
ProgressDialog的樣式有兩種,一種是圓形不明白狀態(tài),一種是水平進(jìn)度條狀態(tài)
第一種方式:圓形進(jìn)度條
final ProgressDialog dialog = new ProgressDialog(this);dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 設(shè)置進(jìn)度條的形式為圓形轉(zhuǎn)動(dòng)的進(jìn)度條dialog.setCancelable(true);// 設(shè)置能否夠通過(guò)點(diǎn)擊Back鍵取消dialog.setCanceledOnTouchOutside(false);// 設(shè)置在點(diǎn)擊Dialog外是否取消Dialog進(jìn)度條dialog.setIcon(R.drawable.ic_launcher);//// 設(shè)置提示的title的圖標(biāo),默認(rèn)是沒(méi)有的,假設(shè)沒(méi)有設(shè)置title的話僅僅設(shè)置Icon是不會(huì)顯示圖標(biāo)的dialog.setTitle("提示");// dismiss監(jiān)聽(tīng)dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {@Overridepublic void onDismiss(DialogInterface dialog) {// TODO Auto-generated method stub}});// 監(jiān)聽(tīng)Key事件被傳遞給dialogdialog.setOnKeyListener(new DialogInterface.OnKeyListener() {@Overridepublic boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {// TODO Auto-generated method stubreturn false;}});// 監(jiān)聽(tīng)cancel事件dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {@Overridepublic void onCancel(DialogInterface dialog) {// TODO Auto-generated method stub}});//設(shè)置可點(diǎn)擊的button,最多有三個(gè)(默認(rèn)情況下)dialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setMessage("這是一個(gè)圓形進(jìn)度條");dialog.show();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {Thread.sleep(5000);// cancel和dismiss方法本質(zhì)都是一樣的,都是從屏幕中刪除Dialog,唯一的差別是// 調(diào)用cancel方法會(huì)回調(diào)DialogInterface.OnCancelListener假設(shè)注冊(cè)的話,dismiss方法不會(huì)回掉dialog.cancel();// dialog.dismiss();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();當(dāng)中通過(guò)Thread.sleep(5000)模擬后臺(tái)操作。
cancel和dismiss方法本質(zhì)都是一樣的,都是從屏幕中刪除Dialog,唯一的差別是:調(diào)用cancel方法會(huì)回調(diào)DialogInterface.OnCancelListener假設(shè)注冊(cè)的話,dismiss方法不會(huì)回掉。
?另外一種方式:水平進(jìn)度條
// 進(jìn)度條還有二級(jí)進(jìn)度條的那種形式,這里就不演示了final ProgressDialog dialog = new ProgressDialog(this);dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 設(shè)置水平進(jìn)度條dialog.setCancelable(true);// 設(shè)置能否夠通過(guò)點(diǎn)擊Back鍵取消dialog.setCanceledOnTouchOutside(false);// 設(shè)置在點(diǎn)擊Dialog外是否取消Dialog進(jìn)度條dialog.setIcon(R.drawable.ic_launcher);// 設(shè)置提示的title的圖標(biāo),默認(rèn)是沒(méi)有的dialog.setTitle("提示");dialog.setMax(100);dialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}});dialog.setMessage("這是一個(gè)水平進(jìn)度條");dialog.show();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubint i = 0;while (i < 100) {try {Thread.sleep(200);// 更新進(jìn)度條的進(jìn)度,能夠在子線程中更新進(jìn)度條進(jìn)度dialog.incrementProgressBy(1);// dialog.incrementSecondaryProgressBy(10)//二級(jí)進(jìn)度條更新方式i++;} catch (Exception e) {// TODO: handle exception}}// 在進(jìn)度條走完時(shí)刪除Dialogdialog.dismiss();}}).start();很多其它的是使用自己定義的ProgressDialog,以滿足不同顯示的須要。
?
? ? ??
轉(zhuǎn)載于:https://www.cnblogs.com/bhlsheji/p/4231800.html
總結(jié)
以上是生活随笔為你收集整理的ProgressDialog使用总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Tomcat数据源总结
- 下一篇: 虚拟机中的solaris无法通过putt