PopupWindow 、AlertDialog和Toast的对比

简介

1、 PopupWindow在android.widget包下,弹出窗口的形式展示。官方文档对该控件的描述是:“一个弹出窗口控件,可以用来显示任意视图(View),而且会浮动在当前 活动(activity)的顶部”。
2、AlertDialog是警告对话框,使用最广泛功能最丰富的一个对话框。 AlertDialog是Dialog的一个直接子类,一个AlertDialog可以有两个Button或者3个Button,可以对一个AlertDialog设置title、message。不能直接通过AlertDialog的构造函数来生成一个AlertDialog,一般生成的时候都是通过它的的一个内部静态类AlertDialog.Builder来构造的。
3、Toast是Android中一种提供给用户简短信息的视图,该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能已不显眼的方式,使用户看到你提供的信息。显示的时间是有限制的,过一段时间后会自动消失,Toast本身可以控制显示时间的长短。

PopupWindow、AlertDialog和Toast的位置设置

(1)PopupWindow

PopupWindow 、AlertDialog和Toast的对比_第1张图片
Paste_Image.png

(2)AlertDialog


PopupWindow 、AlertDialog和Toast的对比_第2张图片
Paste_Image.png

(3)Toast


PopupWindow 、AlertDialog和Toast的对比_第3张图片
Paste_Image.png

二、PopupWindow、Dialog以及Toast的显示与消失
以下是PopupWindow显示和消失方法:

  public void showAtLocation(View parent, int gravity, int x, int y) {
        showAtLocation(parent.getWindowToken(), gravity, x, y);
    }

  public void showAsDropDown(View anchor, int xoff, int yoff) {
        showAsDropDown(anchor, xoff, yoff, DEFAULT_ANCHORED_GRAVITY);
    }

  public void showAsDropDown(View anchor) {
        showAsDropDown(anchor, 0, 0);
    }
dismiss()

以下是Dialog的显示方法:

PopupWindow 、AlertDialog和Toast的对比_第4张图片
Paste_Image.png

消失:


 public void dismiss() {
        if (Looper.myLooper() == mHandler.getLooper()) {
            dismissDialog();
        } else {
            mHandler.post(mDismissAction);
        }
    }

 /**
     * Cancel the dialog.  This is essentially the same as calling {@link #dismiss()}, but it will
     * also call your {@link DialogInterface.OnCancelListener} (if registered).
     */
 public void cancel() {
        if (!mCanceled && mCancelMessage != null) {
            mCanceled = true;
            // Obtain a new message so this dialog can be re-used
            Message.obtain(mCancelMessage).sendToTarget();
        }
        dismiss();
    }

以下是Toast的显示方法:


PopupWindow 、AlertDialog和Toast的对比_第5张图片
Paste_Image.png

消失:


PopupWindow 、AlertDialog和Toast的对比_第6张图片
Paste_Image.png

PopupWindow和Dialog的点击区外不消失:

Dialog
       dialog.setCanceledOnTouchOutside(false);
PopupWindow
       popWindow.setFocusable(false);
       popWindow.setOutsideTouchable(false);//点击外部不消失

自定义创建PopupWindow和AlertDialog的相同代码

   LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   mView=inflater.inflate(R.layout.test_pop_window,null);

设置布局

this.setContentView(mView);
AlertDialog和PopupWindow区别

AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情;而PopupWindow是阻塞式对话框:PopupWindow弹出时,程序会等待,在PopupWindow退出前,程序一直等待,只有当我们调用了dismiss方法的后,PopupWindow退出,程序才会向下执行。
这两种区别的表现是:AlertDialog弹出时,背景是黑色的,但是当我们点击背景,AlertDialog会消失,证明程序不仅响应AlertDialog的操作,还响应其他操作,其他程序没有被阻塞,这说明了AlertDialog是非阻塞式对话框;PopupWindow弹出时,背景没有什么变化,但是当我们点击背景的时候,程序没有响应,只允许我们操作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)Dialog没法设置宽为整个屏幕宽,总有点边界。Popupwindow可以。
(6)二者都有默认的背景,都可以通过setBackgroundDrawable(new ColorDrawable(Android.R.color.transparent);去掉。

你可能感兴趣的:(PopupWindow 、AlertDialog和Toast的对比)