Android 程式開發:(一)詳解活動 —— 1.6 顯示複雜對話方塊

來源:互聯網
上載者:User
除了“對話方塊”進度條,也可以建立一個顯示“操作進度”的對話方塊,例如顯示正在下載的狀態。

1. 建立一個工程:Dialog。

2. main.xml中的代碼。

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btn_dialog3"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:onClick="onClick3"  
  12.         android:text="Click to display a detailed progress dialog" />  
  13.   
  14. </LinearLayout>  

3. DialogActivity.java中的代碼。[java] view plaincopy

  1. public class DialogActivity extends Activity {  
  2.     ProgressDialog progressDialog;  
  3.   
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.     }  
  10.   
  11.     public void onClick3(View v) {  
  12.         showDialog(1);  
  13.         progressDialog.setProgress(0);  
  14.   
  15.         new Thread(new Runnable() {  
  16.             public void run() {  
  17.                 for (int i = 1; i <= 15; i++) {  
  18.                     try {  
  19.                         // ---simulate doing something lengthy---  
  20.                         Thread.sleep(1000);  
  21.                         // ---update the dialog---  
  22.                         progressDialog.incrementProgressBy((int) (100 / 15));  
  23.                     } catch (InterruptedException e) {  
  24.                         e.printStackTrace();  
  25.                     }  
  26.                 }  
  27.                 progressDialog.dismiss();  
  28.             }  
  29.         }).start();  
  30.     }  
  31.   
  32.     @Override  
  33.     protected Dialog onCreateDialog(int id) {  
  34.         switch (id) {  
  35.         case 1:  
  36.             progressDialog = new ProgressDialog(this);  
  37.             progressDialog.setIcon(R.drawable.ic_launcher);  
  38.             progressDialog.setTitle("Downloading files...");  
  39.             progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  40.             progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",  
  41.                     new DialogInterface.OnClickListener() {  
  42.                         public void onClick(DialogInterface dialog,  
  43.                                 int whichButton) {  
  44.                             Toast.makeText(getBaseContext(), "OK clicked!",  
  45.                                     Toast.LENGTH_SHORT).show();  
  46.                         }  
  47.                     });  
  48.             progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",  
  49.                     new DialogInterface.OnClickListener() {  
  50.                         public void onClick(DialogInterface dialog,  
  51.                                 int whichButton) {  
  52.                             Toast.makeText(getBaseContext(), "Cancel clicked!",  
  53.                                     Toast.LENGTH_SHORT).show();  
  54.                         }  
  55.                     });  
  56.             return progressDialog;  
  57.         }  
  58.         return null;  
  59.     }  
  60. }  

4. 按F11調試。

想要建立一個顯示進度的對話方塊,首先要建立一個ProgressDialog類的執行個體,然後設定各種狀態,表徵圖、標題、樣式等等:

[java] view plaincopy
  1. progressDialog = new ProgressDialog(this);  
  2. progressDialog.setIcon(R.drawable.ic_launcher);  
  3. progressDialog.setTitle("Downloading files...");  
  4. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  

然後設定兩個按鈕:[java] view plaincopy

  1. progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",  
  2.         new DialogInterface.OnClickListener() {  
  3.             public void onClick(DialogInterface dialog,  
  4.                     int whichButton) {  
  5.                 Toast.makeText(getBaseContext(), "OK clicked!",  
  6.                         Toast.LENGTH_SHORT).show();  
  7.             }  
  8.         });  
  9. progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",  
  10.         new DialogInterface.OnClickListener() {  
  11.             public void onClick(DialogInterface dialog,  
  12.                     int whichButton) {  
  13.                 Toast.makeText(getBaseContext(), "Cancel clicked!",  
  14.                         Toast.LENGTH_SHORT).show();  
  15.             }  
  16.         });  

使用一個線程顯示進度條的狀態:[java] view plaincopy

  1. progressDialog.setProgress(0);  
  2.   
  3. new Thread(new Runnable() {  
  4.     public void run() {  
  5.         for (int i = 1; i <= 15; i++) {  
  6.             try {  
  7.                 // ---simulate doing something lengthy---  
  8.                 Thread.sleep(1000);  
  9.                 // ---update the dialog---  
  10.                 progressDialog.incrementProgressBy((int) (100 / 15));  
  11.             } catch (InterruptedException e) {  
  12.                 e.printStackTrace();  
  13.             }  
  14.         }  
  15.         progressDialog.dismiss();  
  16.     }  
  17. }).start();  

當進度條到達100%的時候,它就被解除了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.