zoukankan      html  css  js  c++  java
  • Android_Dialog cancle 和dismiss 区别

    AlertDialog使用很方便,但是有一个问题就是:dismiss方法和cancel方法到底有什么不同?

           AlertDialog继承与Dialog,现在各位看看结构图:

          然后在Dialog类中找到了dismiss和cancel方法的实现。重要看dismiss的源码:

    Java代码  收藏代码
    1. public void cancel() {  
    2.        if (mCancelMessage != null) {  
    3.              
    4.            // Obtain a new message so this dialog can be re-used  
    5.            Message.obtain(mCancelMessage).sendToTarget();  
    6.        }  
    7.        dismiss();  
    8.    }  

        看明白了吧! 在cancel方法中调用了dismiss方法。 但是现在还有一个问题就是:mCancelMessage是什么?

        private Message mCancelMessage; // 这是源码中的声明

        然后再来看源码:

    Java代码  收藏代码
    1. public void setOnCancelListener(final OnCancelListener listener) {  
    2.        if (listener != null) {  
    3.            mCancelMessage = mListenersHandler.obtainMessage(CANCEL, listener);  
    4.        } else {  
    5.            mCancelMessage = null;  
    6.        }  
    7.    }  
    8.   
    9. ublic void setCancelMessage(final Message msg) {  
    10.        mCancelMessage = msg;  
    11.    }  

       现在问题清楚了,就是如果你在创建AlertDialog的时候调用了setOnCancelListener 这个mCancelMessage变量有作用,否则dismiss和cancel等同。

    Public void cancel ()

    Since: API Level 1

    Cancel the dialog. This is essentially the same as calling dismiss(), but it will also call yourDialogInterface.OnCancelListener (if registered).

    取消对话框,基本上和调用dismiss效果一样。但是cancel同事也会调用DialogInterface.OnCancelListener注册的事件,如果注册了。

    public void dismiss ()

    Since: API Level 1

    Dismiss this dialog, removing it from the screen. This method can be invoked safely from any thread. Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that inonStop().

    参考:http://blog.csdn.net/cpcpc/article/details/6774823

  • 相关阅读:
    flask---第一篇
    python 奇技淫巧
    Date
    StringBuffer和StringBuilder
    Object
    String 类中的几个练习--获取指定字符串中,大写字母、小写字母、数字的个数||获取一个字符串中,另一个字符串出现的次数
    String 中常用的几种方法
    final
    String类中"=="、equals和普通类中"=="、equals的比较
    构造方法
  • 原文地址:https://www.cnblogs.com/spring87/p/4272541.html
Copyright © 2011-2022 走看看