Dialog详解

Dialog是所有对话框的基类,但Dialog并非继承自View,而是直接从Object构造出来的。Dialog调用是异步调用,所以showDialog()时不会阻碍UI线程。

1. Activity托管对话框:
      Android提供了创建对话框的快捷方式,在Activity中可以通过如showDialog(int dialogId),dismissDialog(int dialogId),onCreateDialog(),onPrepareDialog(),removeDialog()等方法来创建和管理对话框。
      onCreateDialog(int dialogId)和onPrepareDialog(int dialogId, Dialog dialog)是Dialog的2个回调函数,showDialog()触发这两个回调函数的调用。 同所有的onCreate()一样,其只在Dialog第一次生成的时候才会被调用,而onPrepareDialog()在每次执行showDialog()都会被调用(不管Dialog生成了没有)。如果你想要更新对话框的内容,你只要在onPrepareDialog()中作相应的工作就可以了,该方法会在对话框显示之前进行调用。
      dismissDialog()方法是用来关闭对话框的;removeDialog()方法用来将对话框从Activity的托管中移除 (如果对已经移除的对话框重新进行调用showDialog ,则该对话框将进行重新创建)。

2. 常用Dialog
(1)AlertDialog
AlertDialog类是Dialog类的子类,它默认提供了3个按钮和一个文本消息,这些按钮可以按需要来使他们显示或隐藏。AlertDialog类中有一个内部静态类,名为“Builder”,Builder类提供了为对话框添加多选或单选列表,以及为这些列表添加事件处理的功能。另外,这个Builder类将AlertDialog对话框上的3个按钮按照他们的位置分别称呼为:PositiveButton, NeutralButton, NegativeButton。
(2)ProgressDialog
ProgressDialog.dialog = new ProgressDialog(context); 没有内部静态类,直接构造函数构造

3. 一个很特别的Dialog(由Activity转换而来) ,具体请参见参考doc中android.R.style部分
(1)AndroidManifest.xml中的activity的属性中增加:android :theme="@android :style/Theme.Dialog( Activity的对话框主题)。可使Activity变为Dialog(浮动窗口);
(2)AndroidManifest.xml中的activity的属性中增加:android:theme="@android:style/Theme.Translucent"(Activity半透明主题);

4. 示例代码
(1)自定义Dialog并获取Dialog中EditText的数据
Java代码   收藏代码
  1. public class MyDialog extends Dialog implements Button.OnClickListener {  
  2.     private Button okButton, cancelButton;  
  3.     private EditText nameEditText;  
  4.     private MyDialogListener listener;  
  5.       
  6.     public MyDialog(Context context, MyDialogListener listener) {  
  7.         super(context);  
  8.         this.listener = listener;  
  9.     }  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         setContentView(R.layout.mydialog);  
  12.           
  13.         okButton = (Button) findViewById(R.id.okButton);  
  14.         cancelButton = (Button) findViewById(R.id.cancelButton);  
  15.         okButton.setOnClickListener(this);  
  16.         cancelButton.setOnClickListener(this);  
  17.           
  18.         nameEditText = (EditText) findViewById(R.id.nameEditText);  
  19.     }  
  20.     public void onClick(View v) {  
  21.         switch (v.getId()) {  
  22.         case R.id.okButton:  
  23.              listener.onOkClick(nameEditText.getText().toString());  
  24.              dismiss(); // 关闭Dialog  
  25.              break;  
  26.         case R.id.cancelButton:  
  27.              cancel(); // 取消Dialog, "取消"的含义是指不再需要执行对话框上的任何功能和动作, 取消对话框会自动调用dismiss()方法  
  28.              break;  
  29.         }  
  30.         /* 
  31.          * 当用户点击手机设备上的“返回”按钮时,屏幕上的对话框将会被取消, 
  32.          * 如果你想让你的对话框不在这种情况下被取消掉的话,你可以如下设置你的对话框:setCancelable(false); 
  33.          * 对话框的取消和关闭事件可以通过OnCancelListener和OnDismissListener两个监听器来被监听处理。 
  34.          */  
  35.     }  
  36.       
  37. }  

Java代码   收藏代码
  1. public class MainActivity extends Activity implements MyDialogListener {  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.         MyDialog dialog = new MyDialog(thisthis);  
  5.         dialog.show();  
  6.     }  
  7.     public void onCancelClick() {  
  8.     }  
  9.     public void onOkClick(String name) {  
  10.         Toast.makeText(this, name, Toast.LENGTH_LONG).show();  
  11.     }  
  12. }  
  13.   
  14. interface MyDialogListener {  
  15.     public void onOkClick(String name);  
  16.     public void onCancelClick();  
  17. }  

mydialog.xml
Xml代码   收藏代码
  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="wrap_content"  
  5.     android:orientation="vertical">  
  6.     <TextView  
  7.         android:id="@+id/nameMessage"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Enter Name:"></TextView>  
  11.         <EditText  
  12.             android:id="@+id/nameEditText"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="wrap_content"  
  15.             android:textSize="18sp"></EditText>  
  16.         <LinearLayout  
  17.             android:id="@+id/buttonLayout"  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_gravity="center_horizontal">  
  21.             <Button  
  22.                 android:id="@+id/okButton"  
  23.                 android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:text="OK">  
  26.             </Button>  
  27.             <Button  
  28.                 android:id="@+id/cancelButton"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:text="Cancel">  
  32.             </Button>  
  33.      </LinearLayout>  
  34. </LinearLayout>  

Java代码   收藏代码
  1. public class MainActivity2 extends Activity {  
  2.     public void onCreate(Bundle savedInstanceState) {  
  3.         super.onCreate(savedInstanceState);  
  4.           
  5.         AlertDialog dialog = new AlertDialog.Builder(this).create();  
  6.         dialog.setMessage("Do you play cricket?");  
  7.         dialog.setButton("Yes"new DialogInterface.OnClickListener() {  
  8.             public void onClick(DialogInterface dialog, int which) {  
  9.                 switch (which) {  
  10.                 case AlertDialog.BUTTON1:  
  11.                     break;  
  12.                 case AlertDialog.BUTTON2:  
  13.                     break;  
  14.                 }  
  15.             }  
  16.         });  
  17.         dialog.setButton2("No"new DialogInterface.OnClickListener() {  
  18.             public void onClick(DialogInterface dialog, int which) {  
  19.             }  
  20.         });  
  21.         dialog.show();  
  22.     }  
  23. }  

托管Dialog
Java代码   收藏代码
  1. public class MainActivity extends Activity {  
  2.     private Button button1, button2, button3, button4;  
  3.     private Button.OnClickListener listener1, listener2, listener3, listener4;  
  4.     private final int DIALOG1 = 1, DIALOG2 = 2, DIALOG3 = 3, DIALOG4 = 4;  
  5.       
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         prepareListener();  
  11.     }  
  12.       
  13.     protected Dialog onCreateDialog(int id) {  
  14.         switch (id) {  
  15.         case DIALOG1:  
  16.             return buildDialog(this, DIALOG1);  
  17.         case DIALOG2:  
  18.             return buildDialog(this, DIALOG2);  
  19.         case DIALOG3:  
  20.             return buildDialog(this, DIALOG3);  
  21.         case DIALOG4:  
  22.             return buildDialog(this, DIALOG4);  
  23.         }  
  24.         return null;  
  25.     }  
  26.       
  27.     protected void onPrepareDialog(int id, Dialog dialog) {  
  28.         super.onPrepareDialog(id, dialog);  
  29.     }  
  30.       
  31.     private Dialog buildDialog(Context context, int seq) {  
  32.         AlertDialog.Builder builder = new AlertDialog.Builder(context);  
  33.         builder.setIcon(R.drawable.alert_dialog_icon);  
  34.         if (DIALOG1 == seq) {  
  35.             builder.setTitle(R.string.alert_dialog_two_buttons_title);  
  36.             builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  37.                 public void onClick(DialogInterface dialog, int which) {  
  38.                     setTitle(R.string.alert_dialog_ok);  
  39.                 }  
  40.             });  
  41.             builder.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  42.                 public void onClick(DialogInterface dialog, int which) {  
  43.                     setTitle(R.string.alert_dialog_cancel);  
  44.                 }  
  45.             });  
  46.         }  
  47.         if (DIALOG2 == seq) {  
  48.             builder.setTitle(R.string.alert_dialog_two_buttons_msg);  
  49.             builder.setMessage(R.string.alert_dialog_two_buttons2_msg);  
  50.               
  51.             // AlertDialog最多只能有3个Button  
  52.             builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  53.                 public void onClick(DialogInterface dialog, int which) {  
  54.                     setTitle(R.string.alert_dialog_ok);  
  55.                 }  
  56.             });  
  57.             builder.setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {  
  58.                 public void onClick(DialogInterface dialog, int which) {  
  59.                     setTitle(R.string.alert_dialog_something);  
  60.                 }  
  61.             });  
  62.             builder.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  63.                 public void onClick(DialogInterface dialog, int which) {  
  64.                     setTitle(R.string.alert_dialog_cancel);  
  65.                 }  
  66.             });  
  67.         }  
  68.         if (DIALOG3 == seq) {  
  69.             // LayoutInflater的inflate方法可以将一个XML布局变成一个View实例  
  70.             LayoutInflater inflater = LayoutInflater.from(this);  
  71.             View textEntryView = inflater.inflate(R.layout.alert_dialog_text_entry, null);  
  72.             builder.setTitle(R.string.alert_dialog_text_entry);  
  73.               
  74.             // setView()真可以说是Dialog的一个精髓  
  75.             builder.setView(textEntryView);  
  76.             builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  77.                 public void onClick(DialogInterface dialog, int which) {  
  78.                     setTitle(R.string.alert_dialog_ok);  
  79.                 }  
  80.             });  
  81.             builder.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  82.                 public void onClick(DialogInterface dialog, int which) {  
  83.                     setTitle(R.string.alert_dialog_cancel);  
  84.                 }  
  85.             });  
  86.         }  
  87.         if (DIALOG4 == seq) {  
  88.             ProgressDialog dialog = new ProgressDialog(context);  
  89.             dialog.setTitle("Downding the songs...");  
  90.             dialog.setMessage("Please Waiting...");  
  91.             return dialog;  
  92.         }  
  93.         return builder.create();  
  94.     }  
  95.   
  96.     private boolean prepareListener() {  
  97.         // init button  
  98.         button1 = (Button) findViewById(R.id.button1);  
  99.         button2 = (Button) findViewById(R.id.button2);  
  100.         button3 = (Button) findViewById(R.id.button3);  
  101.         button4 = (Button) findViewById(R.id.button4);  
  102.           
  103.         // init listener  
  104.         listener1 = new Button.OnClickListener() {  
  105.             public void onClick(View v) {  
  106.                 showDialog(DIALOG1);  
  107.             }  
  108.         };  
  109.         listener2 = new Button.OnClickListener() {  
  110.             public void onClick(View v) {  
  111.                 showDialog(DIALOG2);  
  112.             }  
  113.         };  
  114.         listener3 = new Button.OnClickListener() {  
  115.             public void onClick(View v) {  
  116.                 showDialog(DIALOG3);  
  117.             }  
  118.         };  
  119.         listener4 = new Button.OnClickListener() {  
  120.             public void onClick(View v) {  
  121.                 showDialog(DIALOG4);  
  122.             }  
  123.         };  
  124.           
  125.         // bind listener  
  126.         button1.setOnClickListener(listener1);  
  127.         button2.setOnClickListener(listener2);  
  128.         button3.setOnClickListener(listener3);  
  129.         button4.setOnClickListener(listener4);  
  130.         return true;  
  131.     }  
  132. }  

alert_dialog_text_entry.xml
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <!-- android:textAppearance:设置字体, 系统自带的字体 -->  
  8.     <!-- android:capitalize:设置大小写;none首字母小写;words首字母大写 -->  
  9.     <TextView android:id="@+id/username_view"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginLeft="20dip"  
  13.         android:layout_marginRight="20dip"  
  14.         android:text="用户名"  
  15.         android:textAppearance="?android:attr/textAppearanceMedium"  
  16.     />  
  17.     <EditText android:id="@+id/username_edit"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_marginLeft="20dip"  
  21.         android:layout_marginRight="20dip"  
  22.         android:capitalize="words"  
  23.         android:textAppearance="?android:attr/textAppearanceMedium"  
  24.     />  
  25.     <TextView android:id="@+id/password_view"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_marginLeft="20dip"  
  29.         android:layout_marginRight="20dip"  
  30.         android:text="密码"  
  31.         android:textAppearance="?android:attr/textAppearanceMedium"  
  32.     />  
  33.     <EditText android:id="@+id/password_edit"  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_marginLeft="20dip"  
  37.         android:layout_marginRight="20dip"  
  38.         android:capitalize="none"  
  39.         android:password="true"  
  40.         android:textAppearance="?android:attr/textAppearanceMedium"  
  41.     />  
  42. </LinearLayout>  


去除对话框Dialog白色边框
使用样式文件,在values 目录下新建styles.xml文件,编写如下代码:

Xml代码   收藏代码
  1. <resources>  
  2.     <style name="dialog" parent="@android:style/Theme.Dialog">  
  3.          <item name="android:windowFrame">@null</item>  
  4.         <item name="android:windowIsFloating">true</item>  
  5.         <item name="android:windowIsTranslucent">false</item>  
  6.         <item name="android:windowNoTitle">true</item>  
  7.         <item name="android:background">@android:color/black</item>  
  8.         <item name="android:windowBackground">@null</item>  
  9.         <item name="android:backgroundDimEnabled">false</item>  
  10.     </style>  
  11. </resources>  

调用时,使用AlerDialog的接口类,Dialog 接口编写如下代码:
Java代码   收藏代码
  1. Dialog dialog = new Dialog(SetActivity.this, R.style.dialog);  
  2.        dialog.setContentView(R.layout.test);  
  3.        dialog.show();  


下面我们查看一下Dialog的源码文件,里面的构造函数为如下:
Java代码   收藏代码
  1. public Dialog(Context context, int theme) {  
  2.         mContext = new ContextThemeWrapper(  
  3.             context, theme == 0 ? com.android.internal.R.style.Theme_Dialog : theme);  
  4.         mWindowManager = (WindowManager)context.getSystemService("window");  
  5.         Window w = PolicyManager.makeNewWindow(mContext);  
  6.         mWindow = w;  
  7.         w.setCallback(this);  
  8.         w.setWindowManager(mWindowManager, nullnull);  
  9.         w.setGravity(Gravity.CENTER);  
  10.         mUiThread = Thread.currentThread();  
  11.         mDismissCancelHandler = new DismissCancelHandler(this);  
  12.     }  


这里面我们可以看出,Android 使用了默认的构造函数为Dialog 设置样式,如果没有为其设置样式,即默认加载事先编写好的样式文件,Dialog 一共由多个9.png的图片构成,大部分都是带有边框的9.png图片,所以就是为什么我们上边的样式文件要将其背景去除掉。
前后效果对比
未设置前:

设置后:


模式对话框Dialog背景的透明度&黑暗度设置方法
设置透明度:
Java代码   收藏代码
  1. WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();    
  2. lp.alpha=1.0f;    
  3. dialog.getWindow().setAttributes(lp);    

alpha在0.0f到1.0f之间。1.0完全不透明,0.0f完全透明
设置黑暗度:
Java代码   收藏代码
  1. dialog.setContentView(R.layout.dialog);    
  2. WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();    
  3. lp.dimAmount=1.0f;    
  4. dialog.getWindow().setAttributes(lp);    
  5. dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);  


Android 之 ProgressDialog
http://blog.csdn.net/dadahacker/archive/2011/02/25/6208368.aspx

Android 对话框(Dialog)大全 建立你自己的对话框
http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值