Android_Dialog cancle 和dismiss 區別

來源:互聯網
上載者:User

標籤:

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

Android_Dialog cancle 和dismiss 區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.