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

  • 相关阅读:
    SGU 495 Kids and Prizes 概率DP 或 数学推理
    poj 2799 IP Networks 模拟 位运算
    uva 202 Repeating Decimals 模拟
    poj 3158 Kickdown 字符串匹配?
    uva 1595 Symmetry 暴力
    uva 201 Squares 暴力
    uva 1594 Ducci Sequence 哈希
    uva 1368 DNA Consensus String 字符串
    数字、字符串、列表的常用操作
    if条件判断 流程控制
  • 原文地址:https://www.cnblogs.com/spring87/p/4272541.html
Copyright © 2011-2022 走看看