Android ProgressDialog使用總結_Android

來源:互聯網
上載者:User

ProgressDialog 繼承自AlertDialog,AlertDialog繼承自Dialog,實現DialogInterface介面。

ProgressDialog的建立方式有兩種,一種是new Dialog ,一種是調用Dialog的靜態方法Dialog.show()。

// 方式一:new Dialog final ProgressDialog dialog = new ProgressDialog(this); dialog.show(); // 方式二:使用靜態方式建立並顯示,這種進度條只能是圓形條,設定title和Message提示內容 ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陸中"); // 方式三 使用靜態方式建立並顯示,這種進度條只能是圓形條,這裡最後一個參數boolean indeterminate設定是否是不明確的狀態 ProgressDialog dialog3 = ProgressDialog .show(this, "提示", "正在登陸中", false); // 方式四 使用靜態方式建立並顯示,這種進度條只能是圓形條,這裡最後一個參數boolean cancelable 設定是否進度條是可以取消的 ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陸中", false, true); // 方式五 使用靜態方式建立並顯示,這種進度條只能是圓形條,這裡最後一個參數 DialogInterface.OnCancelListener // cancelListener用於監聽進度條被取消 ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陸中", true, true, cancelListener); 

方式五中需要一個cancelListener,代碼如下;

private OnCancelListener cancelListener = new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "進度條被取消", Toast.LENGTH_LONG) .show(); } }; 

ProgressDialog的樣式有兩種,一種是圓形不明確狀態,一種是水平進度條狀態

第一種方式:圓形進度條

final ProgressDialog dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 設定進度條的形式為圓形轉動的進度條 dialog.setCancelable(true);// 設定是否可以通過點擊Back鍵取消 dialog.setCanceledOnTouchOutside(false);// 設定在點擊Dialog外是否取消Dialog進度條 dialog.setIcon(R.drawable.ic_launcher);// // 設定提示的title的表徵圖,預設是沒有的,如果沒有設定title的話只設定Icon是不會顯示表徵圖的 dialog.setTitle("提示"); // dismiss監聽 dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { // TODO Auto-generated method stub } }); // 監聽Key事件被傳遞給dialog dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { // TODO Auto-generated method stub return false; } }); // 監聽cancel事件 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub } }); //設定可點擊的按鈕,最多有三個(預設情況下) dialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setMessage("這是一個圓形進度條"); dialog.show(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(5000); // cancel和dismiss方法本質都是一樣的,都是從螢幕中刪除Dialog,唯一的區別是 // 調用cancel方法會回調DialogInterface.OnCancelListener如果註冊的話,dismiss方法不會回掉 dialog.cancel(); // dialog.dismiss(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); 

其中通過Thread.sleep(5000)類比後台操作。

cancel和dismiss方法本質都是一樣的,都是從螢幕中刪除Dialog,唯一的區別是:調用cancel方法會回調DialogInterface.OnCancelListener如果註冊的話,dismiss方法不會回掉。

第二種方式:水平進度條

// 進度條還有二級進度條的那種形式,這裡就不示範了 final ProgressDialog dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 設定水平進度條 dialog.setCancelable(true);// 設定是否可以通過點擊Back鍵取消 dialog.setCanceledOnTouchOutside(false);// 設定在點擊Dialog外是否取消Dialog進度條 dialog.setIcon(R.drawable.ic_launcher);// 設定提示的title的表徵圖,預設是沒有的 dialog.setTitle("提示"); dialog.setMax(100); dialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setMessage("這是一個水平進度條"); dialog.show(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub int i = 0; while (i < 100) { try { Thread.sleep(200); // 更新進度條的進度,可以在子線程中更新進度條進度 dialog.incrementProgressBy(1); // dialog.incrementSecondaryProgressBy(10)//二級進度條更新方式 i++; } catch (Exception e) { // TODO: handle exception } } // 在進度條走完時刪除Dialog dialog.dismiss(); } }).start(); 

關於Android ProgressDialog使用總結的相關知識就給大家介紹到這裡,後續還會持續更新,請持續關注云棲社區網站,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.