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

  • 相关阅读:
    js:数据结构笔记13--检索算法
    ember.js:使用笔记9 开始单元测试
    js:数据结构笔记12--排序算法(2)
    js:数据结构笔记11--排序算法(1)
    js:数据结构笔记10--图和图算法
    js:数据结构笔记9--二叉树
    js:数据结构笔记8--集合
    js:数据结构笔记7--哈希表
    js:数据结构笔记6--字典
    js:数据结构笔记5--链表
  • 原文地址:https://www.cnblogs.com/spring87/p/4272541.html
Copyright © 2011-2022 走看看