AlertDialog,popupWindow,Activity区别

https://blog.csdn.net/android_cmos/article/details/51223776

AlertDialog,popupWindow,Activity区别

AlertDialog builder:用来提示用户一些信息,用起来也比较简单,设置标题类容和按钮即可,如果是加载的自定义的view ,调用 dialog.setView(layout);加载布局即可
popupWindow:就是一个悬浮在Activity之上的窗口,可以用展示任意布局文件。
activity:Activity是Android系统中的四大组件之一,可以用于显示View。Activity是一个与用记交互的系统模块,几乎所有的Activity都是和用户进行交互的

区别:AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情;而PopupWindow是阻塞式对话框:PopupWindow弹出时,程序会等待,在PopupWindow退出前,程序一直等待,只有当我们调用了dismiss方法的后,PopupWindow退出,程序才会向下执行。

1.AlertDialog的基本属性和用法

AlertDialog是Dialog的子类,所以它包含了Dialog类的很多属性和方法,由于AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。

要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

使用AlertDialog.Builder创建对话框需要了解以下几个方法:

setTitle :为对话框设置标题 setIcon :为对话框设置图标 setMessage:为对话框设置内容setView : 给对话框设置自定义样式 setItems :设置对话框要显示的一个list,一般用于显示几个命令时setMultiChoiceItems :用来设置对话框显示一系列的复选框 setNeutralButton  :普通按钮

setPositiveButton  :给对话框添加"Yes"按钮 setNegativeButton :对话框添加"No"按钮create : 创建对话框 show :显示对话框

[java] view plain copy
  1. AlertDialog alertDialog = new AlertDialog.Builder(this).   
  2.             setTitle("对话框的标题").   
  3.             setMessage("对话框的内容").   
  4.             setIcon(R.drawable.ic_launcher).   
  5.             create();   
  6.             alertDialog.show();   

setItems(CharSequence[] items, final OnClickListener listener)方法来实现类似ListView的AlertDialog

第一个参数是要显示的数据的数组,第二个参数是点击某个item的触发事件

[java] view plain copy
  1. final String[] array = new String[] { "悬疑""都市""爱情""动作" ,"惊悚"};   
  2.   
  3.               Dialog alertDialog = new AlertDialog.Builder(this).  
  4.                 setTitle("你喜欢什么样的电影?").  
  5.                 setIcon(R.drawable.ic_launcher)  
  6.                 .setItems(array, new DialogInterface.OnClickListener() {  
  7.                
  8.                  @Override  
  9.                  public void onClick(DialogInterface dialog, int which) {  
  10.                   Toast.makeText(getApplicationContext(), array[which], Toast.LENGTH_SHORT).show();  
  11.                  }  
  12.                 }).  
  13.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  14.   
  15.                  @Override  
  16.                  public void onClick(DialogInterface dialog, int which) {  
  17.                   // TODO Auto-generated method stub  
  18.                  }  
  19.                 }).  
  20.                 create();  
  21.               alertDialog.show();  


setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法来实现类似RadioButton的AlertDialog

第一个参数是要显示的数据的数组,第二个参数是初始值(初始被选中的item),第三个参数是点击某个item的触发事件

[java] view plain copy
  1. final int selectedFruitIndex = 0;  
  2.             final String[] arrayFruit = new String[] { "苹果""橘子""草莓""香蕉" };  
  3.   
  4.               Dialog alertDialog = new AlertDialog.Builder(this).  
  5.                 setTitle("你喜欢吃哪种水果?").  
  6.                 setIcon(R.drawable.ic_launcher)  
  7.                 .setSingleChoiceItems(arrayFruit, 0new DialogInterface.OnClickListener() {  
  8.                
  9.                  @Override  
  10.                  public void onClick(DialogInterface dialog, int which) {  
  11.                   //selectedFruitIndex = which;  
  12.                  }  
  13.                 }).  
  14.                 setPositiveButton("确认"new DialogInterface.OnClickListener() {  
  15.   
  16.                  @Override  
  17.                  public void onClick(DialogInterface dialog, int which) {  
  18.                   Toast.makeText(getApplicationContext(), arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();  
  19.                  }  
  20.                 }).  
  21.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  22.   
  23.                  @Override  
  24.                  public void onClick(DialogInterface dialog, int which) {  
  25.                   // TODO Auto-generated method stub  
  26.                  }  
  27.                 }).  
  28.                 create();  
  29.               alertDialog.show();  


setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法来实现类似CheckBox的AlertDialog
第一个参数是要显示的数据的数组,第二个参数是选中状态的数组,第三个参数是点击某个item的触发事件
[java] view plain copy
  1. final String[] arrayFruit = new String[] { "苹果""橘子""草莓""香蕉" };   
  2.         final boolean[] arrayFruitSelected = new boolean[] {truetruefalsefalse};   
  3.    
  4.         Dialog alertDialog = new AlertDialog.Builder(this).   
  5.                 setTitle("你喜欢吃哪种水果?").   
  6.                 setIcon(R.drawable.ic_launcher)   
  7.                 .setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {   
  8.                        
  9.                     @Override   
  10.                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {   
  11.                         arrayFruitSelected[which] = isChecked;   
  12.                     }   
  13.                 }).   
  14.                 setPositiveButton("确认"new DialogInterface.OnClickListener() {   
  15.    
  16.                     @Override   
  17.                     public void onClick(DialogInterface dialog, int which) {   
  18.                         StringBuilder stringBuilder = new StringBuilder();   
  19.                         for (int i = 0; i < arrayFruitSelected.length; i++) {   
  20.                             if (arrayFruitSelected[i] == true)   
  21.                             {   
  22.                                 stringBuilder.append(arrayFruit[i] + "、");   
  23.                             }   
  24.                         }   
  25.                         Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();   
  26.                     }   
  27.                 }).   
  28.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {   
  29.    
  30.                     @Override   
  31.                     public void onClick(DialogInterface dialog, int which) {   
  32.                         // TODO Auto-generated method stub    
  33.                     }   
  34.                 }).   
  35.                 create();   
  36.         alertDialog.show();   
  37.     }   
  38. }   

实际中,我们也经常会自定义View,来满足我们的开发需求
比如说我们要实现一个Login画面,有用户名和密码,这时我们就要用到自定义View的AlertDialog
[html] 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.     <LinearLayout   
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:gravity="center" >   
  11.    
  12.         <TextView   
  13.             android:layout_width="0dip"   
  14.             android:layout_height="wrap_content"   
  15.             android:layout_weight="1"   
  16.             android:text="@string/user" />   
  17.    
  18.         <EditText   
  19.             android:layout_width="0dip"   
  20.             android:layout_height="wrap_content"   
  21.             android:layout_weight="1" />   
  22.     </LinearLayout>   
  23.    
  24.     <LinearLayout   
  25.         android:layout_width="fill_parent"   
  26.         android:layout_height="wrap_content"   
  27.         android:gravity="center" >   
  28.    
  29.         <TextView   
  30.             android:layout_width="0dip"   
  31.             android:layout_height="wrap_content"   
  32.             android:layout_weight="1"   
  33.             android:text="@string/passward" />   
  34.    
  35.         <EditText   
  36.             android:layout_width="0dip"   
  37.             android:layout_height="wrap_content"   
  38.             android:layout_weight="1" />   
  39.     </LinearLayout>   
  40.    
  41. </LinearLayout>   
然后在Activity中去加载我们的布局
[java] view plain copy
  1. // 取得自定义View    
  2.         LayoutInflater layoutInflater = LayoutInflater.from(this);   
  3.         View myLoginView = layoutInflater.inflate(R.layout.login, null);   
  4.            
  5.         Dialog alertDialog = new AlertDialog.Builder(this).   
  6.                 setTitle("用户登录").   
  7.                 setIcon(R.drawable.ic_launcher).   
  8.                 setView(myLoginView).   
  9.                 setPositiveButton("登录"new DialogInterface.OnClickListener() {   
  10.    
  11.                     @Override   
  12.                     public void onClick(DialogInterface dialog, int which) {   
  13.                         // TODO Auto-generated method stub    
  14.                     }   
  15.                 }).   
  16.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {   
  17.    
  18.                     @Override   
  19.                     public void onClick(DialogInterface dialog, int which) {   
  20.                         // TODO Auto-generated method stub    
  21.                     }   
  22.                 }).   
  23.                 create();   
  24.         alertDialog.show();   
  25.     }   


2.PopupWindow的属性和方法及使用
它可以使用任意布局的View作为其内容 ,这个弹出框是悬浮在当前activity之上的。
    PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
[java] view plain copy
  1. View popView = LayoutInflater.from(this).inflate(  
  2.                     R.layout.pop_style, null);  
  3.             pop = new PopupWindow(popView, 400350);  
  4.             pop.setBackgroundDrawable(new BitmapDrawable());//这些要在show之前设置,否则无法作用  
  5.             pop.setOutsideTouchable(true);  
  6.             Button popButton = (Button) popView.findViewById(R.id.button1);  
  7.             popButton.setOnClickListener(new OnClickListener() {  
  8.                   
  9.                 @Override  
  10.                 public void onClick(View v) {  
  11.                     // TODO 自动生成的方法存根  
  12.                     Log.e("pop----->""bt");  
  13.                 }  
  14.             });  
  15.             pop.showAtLocation(bt2, Gravity.CENTER, 00);  
其中的bt2是点击弹出框相对于它的显示位置
[java] view plain copy
  1. int[] location = new int[2];  
  2.                view.getLocationOnScreen(location);  
  3.                pop.showAtLocation(view, Gravity.NO_GRAVITY, (ScreenUtils.getScreenWidth() - pop.getWidth()) / 2, location[1] - pop.getHeight() + 10);  
修改popwindown显示的位置
注意:有时候比如点击button后天弹出对话框,当点击外部时,弹框消失。这时候可能会遇到一个问题:假如点击的外部区域是button,这个时候就会出现button先消失,后又出现的问题。大多数情况下,我们这时候还是希望点击button时弹框消失就好了,再次点击button时就出现。为此,可以这样做:
1)在你点击弹出popowinddow的view上添加点击事件view.setOnClick(false)
2)在popwind初始化后设置
[java] view plain copy
  1. popupwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {  
  2. @Override  
  3. public void onDismiss() {  
  4. handler.postDelayed(new Runnable() {  
  5. @Override  
  6. public void run() {  
  7. view.setClickable(true);  
  8. }  
  9. }, 100);  
  10. }  
  11. });  

3.AlertDialog和PopupWindow的区别

(1)Popupwindow在显示之前一定要设置宽高,Dialog无此限制。

(2)Popupwindow默认不会响应物理键盘的back,除非显示设置了popup.setFocusable(true);而在点击back的时候,Dialog会消失。

(3)Popupwindow不会给页面其他的部分添加蒙层,而Dialog会。

(4)Popupwindow没有标题,Dialog默认有标题,可以通过dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);取消标题

(5)二者显示的时候都要设置Gravity。如果不设置,Dialog默认是Gravity.CENTER。

(6)二者都有默认的背景,都可以通过setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));去掉。

其中最本质的差别就是:AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情;而PopupWindow是阻塞式对话框:PopupWindow弹出时,程序会等待,在PopupWindow退出前,程序一直等待,只有当我们调用了dismiss方法的后,PopupWindow退出,程序才会向下执行。这两种区别的表现是:AlertDialog弹出时,背景是黑色的,但是当我们点击背景,AlertDialog会消失,证明程序不仅响应AlertDialog的操作,还响应其他操作,其他程序没有被阻塞,这说明了AlertDialog是非阻塞式对话框;PopupWindow弹出时,背景没有什么变化,但是当我们点击背景的时候,程序没有响应,只允许我们操作PopupWindow,其他操作被阻塞。我们在写程序的过程中可以根据自己的需要选择使用Popupwindow或者是Dialog。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值