【Android 开发教程】显示复杂对话框

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


除了“对话框”进度条,也可以创建一个显示“操作进度”的对话框,例如显示正在下载的状态。

1. 创建一个工程:Dialog。

2. main.xml中的代码。

[java] view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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%的时候,它就被解除了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值