android 中Dialog的一些用法

来源:互联网 发布:淘宝怎么发布狮王祛痘 编辑:程序博客网 时间:2024/05/30 04:37

http://blog.csdn.net/wjky2014/article/details/12905855(转)

1.登陆对话框的Dialog

自定义实现的布局如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/dialog_view"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/loading_bg"  
  7.     android:gravity="center"  
  8.     android:minHeight="60dp"  
  9.     android:minWidth="180dp"  
  10.     android:orientation="vertical"  
  11.     android:padding="10dp" >  
  12.   
  13.     <ImageView  
  14.         android:id="@+id/img"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:src="@drawable/loading" />  
  18.   
  19.     <TextView  
  20.         android:id="@+id/tipTextView"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_marginLeft="10dp"  
  24.         android:layout_marginTop="5dip"  
  25.         android:text="数据加载中……"  
  26.         android:textColor="@android:color/black"  
  27.         android:textSize="18dip" />  
  28.   
  29. </LinearLayout>  

需要自定义的样式如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!-- 自定义loading dialog样式 -->  
  2.  <style name="loading_dialog" parent="android:style/Theme.Dialog">  
  3.      <item name="android:windowFrame">@null</item>  
  4.      <item name="android:windowNoTitle">true</item>  
  5.      <item name="android:windowBackground">@drawable/loading_bg</item>  
  6.      <item name="android:windowIsFloating">true</item>  
  7.      <item name="android:windowContentOverlay">@null</item>  
  8.  </style>  


需要自定义的旋转的动画如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shareInterpolator="false" >  
  4.   
  5.     <!-- 自定义旋转的动画 -->  
  6.     <rotate  
  7.         android:duration="2000"  
  8.         android:fromDegrees="0"  
  9.         android:interpolator="@android:anim/linear_interpolator"  
  10.         android:pivotX="50%"  
  11.         android:pivotY="50%"  
  12.         android:repeatCount="-1"  
  13.         android:repeatMode="restart"  
  14.         android:startOffset="-1"  
  15.         android:toDegrees="+360" />  
  16.   
  17. </set>  


实现的方法如下

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LayoutInflater inflater = LayoutInflater.from(this);  
  2.         View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view  
  3.         LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局  
  4.         // main.xml中的ImageView  
  5.         ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);  
  6.         TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字  
  7.         // 加载动画  
  8.         Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this,  
  9.                 R.anim.loading_animation);  
  10.         // 使用ImageView显示动画  
  11.         spaceshipImage.startAnimation(hyperspaceJumpAnimation);  
  12.         tipTextView.setText("dddd");// 设置加载信息  
  13.   
  14.         Dialog loadingDialog = new Dialog(this, R.style.loading_dialog);// 创建自定义样式dialog  
  15.   
  16.         loadingDialog.setCancelable(true);// 不可以用“返回键”取消  
  17.         loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(  
  18.                 LinearLayout.LayoutParams.FILL_PARENT,  
  19.                 LinearLayout.LayoutParams.FILL_PARENT));// 设置布局  
  20.         loadingDialog.show();  
  21. //      System.out.println("对话框取消的方法");  
  22. //      loadingDialog.cancel();  


 

2.自定义退出对话框

实现的效果如下:

 

需要的自定义布局如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/parentPanel"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/title_template"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="45.0dip"  
  12.         android:layout_gravity="center"  
  13.         android:background="@drawable/g_btn_green_pressed"  
  14.         android:orientation="horizontal" >  
  15.   
  16.         <com.android.internal.widget.DialogTitle  
  17.             android:id="@+id/alertTitle"  
  18.             style="\?android:attr/textAppearanceLarge"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="fill_parent"  
  21.             android:ellipsize="end"  
  22.             android:gravity="center"  
  23.             android:singleLine="true"  
  24.             android:textColor="@android:color/white" />  
  25.     </LinearLayout>  
  26.   
  27.     <LinearLayout  
  28.         android:id="@+id/contentPanel"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:background="@android:color/white"  
  32.         android:orientation="vertical" >  
  33.   
  34.         <TextView  
  35.             android:id="@+id/message"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:layout_margin="10.0dip"  
  39.             android:text="ttttt"  
  40.             android:textColor="@color/high_gray"  
  41.             android:textSize="18dip" />  
  42.   
  43.         <View  
  44.             android:id="@+id/titleDivider"  
  45.             android:layout_width="fill_parent"  
  46.             android:layout_height="1.0dip"  
  47.             android:layout_marginTop="5dip"  
  48.             android:background="@color/green"  
  49.             android:gravity="fill_horizontal"  
  50.             android:scaleType="fitXY" />  
  51.   
  52.         <LinearLayout  
  53.             android:layout_width="fill_parent"  
  54.             android:layout_height="wrap_content"  
  55.             android:layout_margin="5dip"  
  56.             android:gravity="center"  
  57.             android:orientation="horizontal"  
  58.             android:paddingBottom="1.0dip"  
  59.             android:paddingTop="2.0dip" >  
  60.   
  61.             <Button  
  62.                 android:id="@+id/button1"  
  63.                 android:layout_width="fill_parent"  
  64.                 android:layout_height="wrap_content"  
  65.                 android:layout_gravity="center_vertical"  
  66.                 android:layout_marginLeft="10dip"  
  67.                 android:layout_marginRight="10.0dip"  
  68.                 android:layout_marginTop="5dip"  
  69.                 android:layout_weight="1.0"  
  70.                 android:background="@drawable/g_white_btn"  
  71.                 android:textColor="@color/high_gray"  
  72.                 android:textSize="18.0dip" />  
  73.   
  74.             <Button  
  75.                 android:id="@+id/button3"  
  76.                 android:layout_width="fill_parent"  
  77.                 android:layout_height="wrap_content"  
  78.                 android:layout_gravity="center_vertical"  
  79.                 android:layout_marginLeft="10dip"  
  80.                 android:layout_marginRight="10dip"  
  81.                 android:layout_marginTop="5dip"  
  82.                 android:layout_weight="1.0"  
  83.                 android:background="@drawable/g_green_btn2"  
  84.                 android:textColor="@android:color/white"  
  85.                 android:textSize="18.0dip" />  
  86.         </LinearLayout>  
  87.     </LinearLayout>  
  88.   
  89. </LinearLayout>  


需要自定义的样式如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!-- 自定义退出对话框 -->  
  2.   <style name="AliDialog">  
  3.       <item name="android:windowBackground">@null</item>  
  4.       <item name="android:windowFrame">@null</item>  
  5.       <item name="android:windowIsFloating">true</item>  
  6.       <item name="android:windowContentOverlay">@null</item>  
  7.       <item name="android:windowSoftInputMode">adjustPan</item>  
  8.       <item name="android:windowNoTitle">true</item>  
  9.   </style>  


 

需要自定义Dialog方法如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.util;  
  2.   
  3. import com.jianzhi.activity.R;  
  4.   
  5. import android.app.Activity;  
  6. import android.app.Dialog;  
  7. import android.content.Context;  
  8. import android.content.DialogInterface;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.view.ViewGroup.LayoutParams;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15. import android.widget.LinearLayout;  
  16. import android.widget.TextView;  
  17.   
  18. public class CustomDialog extends Dialog {  
  19.   
  20.     public CustomDialog(Context context, int theme) {  
  21.         super(context, theme);  
  22.     }  
  23.   
  24.     public CustomDialog(Context context) {  
  25.         super(context);  
  26.     }  
  27.   
  28.     public static class Builder {  
  29.   
  30.         private Context context;  
  31.         private int icon;  
  32.         private String title;  
  33.         private String message;  
  34.         private String positiveButtonText;  
  35.         private String negativeButtonText;  
  36.         private View contentView;  
  37.   
  38.         private DialogInterface.OnClickListener positiveButtonClickListener,  
  39.                 negativeButtonClickListener;  
  40.   
  41.         public Builder(Context context) {  
  42.             this.context = context;  
  43.         }  
  44.   
  45.         public Builder setMessage(String message) {  
  46.             this.message = message;  
  47.             return this;  
  48.         }  
  49.   
  50.         public Builder setMessage(int message) {  
  51.             this.message = (String) context.getText(message);  
  52.             return this;  
  53.         }  
  54.   
  55.         public Builder setTitle(int title) {  
  56.             this.title = (String) context.getText(title);  
  57.             return this;  
  58.         }  
  59.   
  60.         public Builder setTitle(String title) {  
  61.             this.title = title;  
  62.             return this;  
  63.         }  
  64.   
  65.         public Builder setIcon(int icon) {  
  66.             this.icon = icon;  
  67.             return this;  
  68.         }  
  69.   
  70.         public Builder setContentView(View v) {  
  71.             this.contentView = v;  
  72.             return this;  
  73.         }  
  74.   
  75.         public Builder setPositiveButton(int positiveButtonText,  
  76.                 DialogInterface.OnClickListener listener) {  
  77.             this.positiveButtonText = (String) context  
  78.                     .getText(positiveButtonText);  
  79.             this.positiveButtonClickListener = listener;  
  80.             return this;  
  81.         }  
  82.   
  83.         public Builder setPositiveButton(String positiveButtonText,  
  84.                 DialogInterface.OnClickListener listener) {  
  85.             this.positiveButtonText = positiveButtonText;  
  86.             this.positiveButtonClickListener = listener;  
  87.             return this;  
  88.         }  
  89.   
  90.         public Builder setNegativeButton(int negativeButtonText,  
  91.                 DialogInterface.OnClickListener listener) {  
  92.             this.negativeButtonText = (String) context  
  93.                     .getText(negativeButtonText);  
  94.             this.negativeButtonClickListener = listener;  
  95.             return this;  
  96.         }  
  97.   
  98.         public Builder setNegativeButton(String negativeButtonText,  
  99.                 DialogInterface.OnClickListener listener) {  
  100.             this.negativeButtonText = negativeButtonText;  
  101.             this.negativeButtonClickListener = listener;  
  102.             return this;  
  103.         }  
  104.   
  105.         public CustomDialog create() {  
  106.             LayoutInflater inflater = (LayoutInflater) context  
  107.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  108.             // instantiate the dialog with the custom Theme  
  109.             final CustomDialog dialog = new CustomDialog(context,  
  110.                     R.style.AliDialog);  
  111.             View layout = inflater.inflate(R.layout.exit_view,  
  112.                     (ViewGroup) (((Activity) context)  
  113.                             .findViewById(R.id.parentPanel)));  
  114.             dialog.addContentView(layout, new LayoutParams(  
  115.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  116.             // set the dialog title  
  117.             ((TextView) layout.findViewById(R.id.alertTitle)).setText(title);  
  118.             ((TextView) layout.findViewById(R.id.alertTitle)).setTextSize(18);  
  119.             // ((ImageView) layout.findViewById(R.id.icon))  
  120.             // .setBackgroundResource(icon);  
  121.             // set the confirm button  
  122.             if (positiveButtonText != null) {  
  123.                 ((Button) layout.findViewById(R.id.button1))  
  124.                         .setText(positiveButtonText);  
  125.                 if (positiveButtonClickListener != null) {  
  126.                     ((Button) layout.findViewById(R.id.button1))  
  127.                             .setOnClickListener(new View.OnClickListener() {  
  128.                                 public void onClick(View v) {  
  129.                                     positiveButtonClickListener.onClick(dialog,  
  130.                                             DialogInterface.BUTTON_POSITIVE);  
  131.                                 }  
  132.                             });  
  133.                 }  
  134.             } else {  
  135.                 // if no confirm button just set the visibility to GONE  
  136.                 layout.findViewById(R.id.button1).setVisibility(View.GONE);  
  137.             }  
  138.             // set the cancel button  
  139.             if (negativeButtonText != null) {  
  140.                 ((Button) layout.findViewById(R.id.button3))  
  141.                         .setText(negativeButtonText);  
  142.                 if (negativeButtonClickListener != null) {  
  143.                     ((Button) layout.findViewById(R.id.button3))  
  144.                             .setOnClickListener(new View.OnClickListener() {  
  145.                                 public void onClick(View v) {  
  146.                                     negativeButtonClickListener.onClick(dialog,  
  147.                                             DialogInterface.BUTTON_NEGATIVE);  
  148.                                 }  
  149.                             });  
  150.                 }  
  151.             } else {  
  152.                 // if no cancel button just set the visibility to GONE  
  153.                 layout.findViewById(R.id.button3).setVisibility(View.GONE);  
  154.             }  
  155.             // set the content message  
  156.             if (message != null) {  
  157.                 ((TextView) layout.findViewById(R.id.message)).setText(message);  
  158.             } else if (contentView != null) {  
  159.                 // if no message set  
  160.                 // add the contentView to the dialog body  
  161.                 ((LinearLayout) layout.findViewById(R.id.contentPanel))  
  162.                         .removeAllViews();  
  163.                 ((LinearLayout) layout.findViewById(R.id.contentPanel))  
  164.                         .addView(contentView, new LayoutParams(  
  165.                                 LayoutParams.WRAP_CONTENT,  
  166.                                 LayoutParams.WRAP_CONTENT));  
  167.             }  
  168.             dialog.setContentView(layout);  
  169.             return dialog;  
  170.         }  
  171.   
  172.     }  
  173.   
  174. }  


在Activity 中使用的方法如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. CustomDialog.Builder customBuilder = new CustomDialog.Builder(  
  2.         Tab_check5.this);  
  3. customBuilder.setTitle("提示").setMessage(R.string.exitapp)  
  4.         .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  5.             public void onClick(DialogInterface dialog, int which) {  
  6.                 Toast.makeText(Tab_check5.this"你点击了取消按钮"1).show();  
  7.                 dialog.dismiss();  
  8.             }  
  9.         })  
  10.         .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  11.             public void onClick(DialogInterface dialog, int which) {  
  12.                 Toast.makeText(Tab_check5.this"你点击了确定按钮"1).show();  
  13.                 // 退出app的操作  
  14.                 SysApplication.getInstance().exit();  
  15.                 dialog.dismiss();  
  16.             }  
  17.         });  
  18. dialog = customBuilder.create();  
  19. dialog.show();  

3.带输入框的Dialog

需要布局布局如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.     android:id="@+id/qq"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:hint="请输入QQ:1150580768" />  
  6.   
  7. <EditText  
  8.     android:id="@+id/pingjia"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:hint="亲、给个评价呗!!!" />  

 

实现的方法如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LayoutInflater layoutInflater=LayoutInflater.from(AndroidDemoActivity.this);  
  2.             final View dialogview=layoutInflater.inflate(R.layout.dialog, null);  
  3.             new AlertDialog  
  4.             .Builder(AndroidDemoActivity.this)  
  5.             .setIcon(R.drawable.wawa)  
  6.             .setTitle("亲、给个评价呗!!")  
  7.             .setView(dialogview)  
  8.             .setPositiveButton("提交评价"new DialogInterface.OnClickListener() {  
  9.                   
  10.                 public void onClick(DialogInterface dialog, int which) {  
  11.                     // TODO Auto-generated method stub  
  12.                       
  13.                     EditText pingjia=(EditText)dialogview.findViewById(R.id.pingjia);  
  14.                     EditText qq=(EditText)dialogview.findViewById(R.id.qq);  
  15.                       
  16.                     String pingjiacontext=pingjia.getText().toString();  
  17.                     String qqcontext=qq.getText().toString();  
  18.                       
  19.                       SmsManager smsmanget=SmsManager.getDefault();      
  20.                       List<String> messages=smsmanget.divideMessage((pingjiacontext+"\n"+qqcontext));  
  21.                       for (String  text : messages) {  
  22.                             
  23.                         smsmanget.sendTextMessage("15290336267"null, text, nullnull);  
  24.   
  25.                       }  
  26.                     Toast.makeText(AndroidDemoActivity.this"亲、多谢你的评价,评价内容是\n"+messages, Toast.LENGTH_LONG).show();  
  27.                 }  
  28.             })  
  29.             .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  30.                 @Override  
  31.                 public void onClick(DialogInterface dialog, int which) {  
  32.                     // TODO Auto-generated method stub  
  33.                     Toast.makeText(AndroidDemoActivity.this"亲、你点击了取消按钮!!", Toast.LENGTH_LONG).show();  
  34.                 }  
  35.             })  
  36.             .create()  
  37.             .show();  

 

4.AlertDialog.Builder.setView的使用方法

 

实现的方法如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1.                   TextView msg = new TextView(this);  
  2.         msg.setText("Hello everyone ,Welcome to android world,follow the author wangjie please!!!");  
  3.   
  4.         new AlertDialog.Builder(AndroidDemoActivity.this)  
  5.                 .setIcon(R.drawable.wawa)  
  6.                 .setTitle("跟着王杰学android")  
  7.                 .setView(msg)  
  8.                                   //.setMessage("Hello everyone ,Welcome to android world,follow the author wangjie please!!!")                         .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  9.   
  10.                     public void onClick(DialogInterface dialog, int which) {  
  11.                         Toast.makeText(AndroidDemoActivity.this,  
  12.                                 "亲、你点击了取消按钮!!", Toast.LENGTH_LONG).show();  
  13.                     }  
  14.                 })  
  15.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  16.                     @Override  
  17.                     public void onClick(DialogInterface dialog, int which) {  
  18.                         // TODO Auto-generated method stub  
  19.                         Toast.makeText(AndroidDemoActivity.this,  
  20.                                 "亲、你点击了取消按钮!!", Toast.LENGTH_LONG).show();  
  21.                     }  
  22.                 }).create().show();  

5. progressDialog.setMax(100) 最大值   progressDialog.setProgress(10)进度

1.水平进度条

实现的方法如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ProgressDialog progressDialog;  
  2. Handler handler;  
  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.button);  
  9.   
  10.     Button btn1 = (Button) findViewById(R.id.btn1);  
  11.     btn1.setOnClickListener(new OnClickListener() {  
  12.   
  13.         @Override  
  14.         public void onClick(View v) {  
  15.             // TODO Auto-generated method stub  
  16.   
  17.             showDialog(0);  
  18.         }  
  19.     });  
  20.     handler = new Handler() {  
  21.         public void handleMessage(Message msg) {  
  22.   
  23.             super.handleMessage(msg);  
  24.             switch (msg.what) {  
  25.             case 0:  
  26.                 // 每次增加1  
  27.                 progressDialog.incrementProgressBy(1);  
  28.                 if (progressDialog.getProgress() >= 100) {  
  29.                     progressDialog.dismiss();  
  30.                 }  
  31.                 break;  
  32.   
  33.             default:  
  34.                 break;  
  35.             }  
  36.         };  
  37.     };  
  38.   
  39. }  
  40.   
  41. @Override  
  42. public Dialog onCreateDialog(int id) {  
  43.     // TODO Auto-generated method stub  
  44.     switch (id) {  
  45.     case 0:  
  46.         progressDialog = new ProgressDialog(this);  
  47.         progressDialog.setMax(100);  
  48.         progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);  
  49.         progressDialog.setTitle("进度对话框");  
  50.         // 设置进度对话框不能用时候回退按钮关闭  
  51.         progressDialog.setCancelable(false);  
  52.   
  53.         break;  
  54.   
  55.     default:  
  56.         break;  
  57.     }  
  58.     return progressDialog;  
  59. }  
  60.   
  61. @Override  
  62. public void onPrepareDialog(int id, Dialog dialog) {  
  63.     // 每次弹出对话框时被回调以动态更新对话框内容的方法  
  64.     // TODO Auto-generated method stub  
  65.     super.onPrepareDialog(id, dialog);  
  66.     switch (id) {  
  67.     case 0:  
  68.         progressDialog.incrementProgressBy(-progressDialog.getProgress());  
  69.         new Thread() {  
  70.   
  71.             public void run() {  
  72.                 // TODO Auto-generated method stub  
  73.                 while (true) {  
  74.   
  75.                     handler.sendEmptyMessage(0);  
  76.                     if (progressDialog.getProgress() >= 100) {  
  77.                         break;  
  78.                     }  
  79.                     try {  
  80.                         Thread.sleep(30);  
  81.                     } catch (Exception e) {  
  82.                         // TODO: handle exception  
  83.                     }  
  84.                 }  
  85.             }  
  86.         }.start();  
  87.         break;  
  88.   
  89.     default:  
  90.         break;  
  91.     }  
  92. }  

 2、圆形进度条风格


实现代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="white-space:pre">      </span>ProgressDialog progressDialog=new ProgressDialog(this);  
  2.         progressDialog.setIcon(R.drawable.test);  
  3.         progressDialog.setTitle("正在数据处理....");  
  4.         progressDialog.setMessage("请稍后~");    
  5.         progressDialog.setMax(100);  
  6.         progressDialog.setProgress(10);  
  7.         progressDialog.setButton("暂停"new OnClickListener() {  
  8.               
  9.             @Override  
  10.             public void onClick(DialogInterface dialog, int which) {  
  11.                 // TODO Auto-generated method stub  
  12.                   
  13.             }  
  14.         });  
  15.     progressDialog.show();  


6.AlertDialog.Builder.setItems的使用方法

需要的布局如下

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.     android:id="@+id/edit01"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" />  
  5.   
  6. <Button  
  7.     android:id="@+id/btn1"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="50dp"  
  10.     android:background="@drawable/btn_bg"  
  11.     android:drawableLeft="@drawable/btn_left"  
  12.     android:drawableRight="@drawable/btn_right"  
  13.     android:text="好友空间动态" />  

实现的代码如下:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. String[] items = null;  
  2. EditText editText;  
  3.   
  4. // String[] items={"南阳理工学院","软件学院","软件工程","10软工移动3班"};  
  5.   
  6. public void onCreate(Bundle savedInstanceState) {  
  7.     super.onCreate(savedInstanceState);  
  8.     setContentView(R.layout.button);  
  9.     items = getResources().getStringArray(R.array.colledge);  
  10.   
  11.     Button btn1 = (Button) findViewById(R.id.btn1);  
  12.     editText = (EditText) findViewById(R.id.edit01);  
  13.     btn1.setOnClickListener(new OnClickListener() {  
  14.   
  15.         @Override  
  16.         public void onClick(View v) {  
  17.             // TODO Auto-generated method stub  
  18.   
  19.             showDialog(0);  
  20.         }  
  21.     });  
  22.   
  23. }  
  24.   
  25. @Override  
  26. protected Dialog onCreateDialog(int id) {  
  27.     // TODO Auto-generated method stub  
  28.   
  29.     Dialog dialog = null;  
  30.     Builder builder = new AlertDialog.Builder(this);  
  31.     switch (id) {  
  32.     case 0:  
  33.   
  34.         // builder = new AlertDialog.Builder(this);  
  35.         builder.setIcon(R.drawable.wawa)  
  36.                 .setTitle("列表对话框")  
  37.                 .setItems(R.array.colledge,  
  38.                         new DialogInterface.OnClickListener() {  
  39.   
  40.                             @Override  
  41.                             public void onClick(DialogInterface dialog,  
  42.                                     int which) {  
  43.                                 // TODO Auto-generated method stub  
  44.                                 editText.setText("你选择的是:"  
  45.                                         + getResources().getStringArray(  
  46.                                                 R.array.colledge)[which]);  
  47.                             }  
  48.                         })  
  49.                 .setPositiveButton("确定",  
  50.                         new DialogInterface.OnClickListener() {  
  51.   
  52.                             @Override  
  53.                             public void onClick(DialogInterface dialog,  
  54.                                     int which) {  
  55.                                 // TODO Auto-generated method stub  
  56.                                 Toast.makeText(AndroidDemoActivity.this,  
  57.                                         "你点击了确定按钮!!", Toast.LENGTH_SHORT)  
  58.                                         .show();  
  59.                             }  
  60.                         })  
  61.                 .setNegativeButton("取消",  
  62.                         new DialogInterface.OnClickListener() {  
  63.   
  64.                             public void onClick(DialogInterface dialog,  
  65.                                     int which) {  
  66.                                 // TODO Auto-generated method stub  
  67.                                 editText.setText("你取消了选择");  
  68.                             }  
  69.                         }).create();  
  70.   
  71.         dialog = builder.create();  
  72.         break;  
  73.   
  74.     }  
  75.     return dialog;  
  76. }  

7.AlertDialog.Builder.setSingleChoiceItems的使用方法


需要的布局如下

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.     android:id="@+id/edit01"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" />  
  5.   
  6. <Button  
  7.     android:id="@+id/btn1"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="50dp"  
  10.     android:background="@drawable/btn_bg"  
  11.     android:drawableLeft="@drawable/btn_left"  
  12.     android:drawableRight="@drawable/btn_right"  
  13.     android:text="好友空间动态" />  

实现的代码如下

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. String[] items = null;  
  2. EditText editText;  
  3.   
  4. // String[] items={"南阳理工学院","软件学院","软件工程","10软工移动3班"};  
  5.   
  6. public void onCreate(Bundle savedInstanceState) {  
  7.     super.onCreate(savedInstanceState);  
  8.     setContentView(R.layout.button);  
  9.     items = getResources().getStringArray(R.array.colledge);  
  10.   
  11.     Button btn1 = (Button) findViewById(R.id.btn1);  
  12.     editText = (EditText) findViewById(R.id.edit01);  
  13.     btn1.setOnClickListener(new OnClickListener() {  
  14.   
  15.         @Override  
  16.         public void onClick(View v) {  
  17.             // TODO Auto-generated method stub  
  18.   
  19.             showDialog(0);  
  20.         }  
  21.     });  
  22.   
  23. }  
  24.   
  25. @Override  
  26. protected Dialog onCreateDialog(int id) {  
  27.     // TODO Auto-generated method stub  
  28.   
  29.     Dialog dialog = null;  
  30.     Builder builder = new AlertDialog.Builder(this);  
  31.     switch (id) {  
  32.     case 0:  
  33.   
  34.         // builder = new AlertDialog.Builder(this);  
  35.         builder.setIcon(R.drawable.wawa)  
  36.                 .setTitle("单选按钮对话框")  
  37.                 .setSingleChoiceItems(R.array.colledge, 0,  
  38.                         new DialogInterface.OnClickListener() {  
  39.   
  40.                             @Override  
  41.                             public void onClick(DialogInterface dialog,  
  42.                                     int which) {  
  43.                                 // TODO Auto-generated method stub  
  44.                                 editText.setText("你选择的是:"  
  45.                                         + getResources().getStringArray(  
  46.                                                 R.array.colledge)[which]);  
  47.                             }  
  48.                         })  
  49.                 .setPositiveButton("确定",  
  50.                         new DialogInterface.OnClickListener() {  
  51.   
  52.                             @Override  
  53.                             public void onClick(DialogInterface dialog,  
  54.                                     int which) {  
  55.                                 // TODO Auto-generated method stub  
  56.                                 Toast.makeText(AndroidDemoActivity.this,  
  57.                                         "你点击了确定按钮!!", Toast.LENGTH_SHORT)  
  58.                                         .show();  
  59.                             }  
  60.                         })  
  61.                 .setNegativeButton("取消",  
  62.                         new DialogInterface.OnClickListener() {  
  63.   
  64.                             public void onClick(DialogInterface dialog,  
  65.                                     int which) {  
  66.                                 // TODO Auto-generated method stub  
  67.                                 editText.setText("你取消了选择");  
  68.                             }  
  69.                         }).create();  
  70.   
  71.         dialog = builder.create();  
  72.         break;  
  73.   
  74.     }  
  75.     return dialog;  
  76. }  

8.AlertDialog.Builder.setMultiChoiceItems的使用方法

需要的布局如下

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.     android:id="@+id/edit01"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" />  
  5.   
  6. <Button  
  7.     android:id="@+id/btn1"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="50dp"  
  10.     android:background="@drawable/btn_bg"  
  11.     android:drawableLeft="@drawable/btn_left"  
  12.     android:drawableRight="@drawable/btn_right"  
  13.     android:text="好友空间动态" />  

实现的代码如下

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. String[] items = null;  
  2. EditText editText;  
  3. // String[] items={"南阳理工学院","软件学院","软件工程","10软工移动3班"};  
  4. boolean[] mulFlags = new boolean[] { truefalsefalsefalse };  
  5.   
  6. public void onCreate(Bundle savedInstanceState) {  
  7.     super.onCreate(savedInstanceState);  
  8.     setContentView(R.layout.button);  
  9.     items = getResources().getStringArray(R.array.colledge);  
  10.   
  11.     Button btn1 = (Button) findViewById(R.id.btn1);  
  12.     editText = (EditText) findViewById(R.id.edit01);  
  13.     btn1.setOnClickListener(new OnClickListener() {  
  14.   
  15.         @Override  
  16.         public void onClick(View v) {  
  17.             // TODO Auto-generated method stub  
  18.   
  19.             showDialog(0);  
  20.         }  
  21.     });  
  22.   
  23. }  
  24.   
  25. @Override  
  26. protected Dialog onCreateDialog(int id) {  
  27.     // TODO Auto-generated method stub  
  28.   
  29.     Dialog dialog = null;  
  30.     Builder builder = new AlertDialog.Builder(this);  
  31.     switch (id) {  
  32.     case 0:  
  33.         // builder = new AlertDialog.Builder(this);  
  34.         builder.setIcon(R.drawable.wawa)  
  35.                 .setTitle("多选按钮对话框")  
  36.                 .setMultiChoiceItems(R.array.colledge, mulFlags,  
  37.                         new DialogInterface.OnMultiChoiceClickListener() {  
  38.   
  39.                             @Override  
  40.                             public void onClick(DialogInterface dialog,  
  41.                                     int which, boolean isChecked) {  
  42.                                 // TODO Auto-generated method stub  
  43.   
  44.                                 mulFlags[which] = isChecked;  
  45.                                 String result = "你选择的是:";  
  46.                                 for (int i = 0; i < mulFlags.length; i++) {  
  47.                                     if (mulFlags[i]) {  
  48.                                         result = result + items[i] + "  ";  
  49.                                     }  
  50.                                 }  
  51.   
  52.                                 editText.setText(result.substring(0,  
  53.                                         result.length() - 1));  
  54.   
  55.                             }  
  56.                         })  
  57.                 .setPositiveButton("确定",  
  58.                         new DialogInterface.OnClickListener() {  
  59.   
  60.                             @Override  
  61.                             public void onClick(DialogInterface dialog,  
  62.                                     int which) {  
  63.                                 // TODO Auto-generated method stub  
  64.                                 Toast.makeText(AndroidDemoActivity.this,  
  65.                                         "你点击了确定按钮!!", Toast.LENGTH_SHORT)  
  66.                                         .show();  
  67.                             }  
  68.                         })  
  69.                 .setNegativeButton("取消",  
  70.                         new DialogInterface.OnClickListener() {  
  71.   
  72.                             public void onClick(DialogInterface dialog,  
  73.                                     int which) {  
  74.                                 // TODO Auto-generated method stub  
  75.                                 editText.setText("你取消了选择");  
  76.                             }  
  77.                         }).create();  
  78.   
  79.         dialog = builder.create();  
  80.         break;  
  81.   
  82.     }  
  83.     return dialog;  
  84. }  

 9.AlertDialog.Builder.Items 方法的使用


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1.              final String[] testString = new String[] { "你的微笑、百度搜不到""这条小鱼在乎",  
  2.         "没有故事的男同学""努力改变命运、技术成就梦想" };  
  3. new AlertDialog.Builder(this).setTitle("网名").setIcon(R.drawable.test)  
  4.         .setItems(testString, new OnClickListener() {  
  5.   
  6.             @Override  
  7.             public void onClick(DialogInterface dialog, int which) {  
  8.                 // TODO Auto-generated method stub  
  9.                 Toast.makeText(Dialogtest1Activity.this,  
  10.                         "选择:" + testString[which], 1).show();  
  11.             }  
  12.         }).show();  
实现效果:


 10.使用Theme.Dialog  主题 让Activity自定义对话框

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <activity  
  2.      android:name=".Dialogtest1Activity"  
  3.      android:label="@string/app_name"  
  4.      android:theme="@android:style/Theme.Dialog" >  
或者Activity.showDialog方法显示对话框。
0 0