AlertDialog.Builder和AlertDialog之間的關係

在沒有正確認識這兩個關係之前,做了一個自定義佈局添加到對話框中,自定義佈局中的按鈕的監聽裏面寫的讓對話框消失的功能無法實現,因爲使用了AlerDialog.Builder而沒有使用builder.create()來創建對話框,查看了API發現:

/**
 * Set a custom view to be the contents of the Dialog. If the  supplied view is an instance of a ListView the light background will be used  */
//將自定義佈局作爲對話框的內容進行展示
public AlertDialog.Builder setView (View view)

/**Set the view to display in that dialog. */
//將自定義佈局以對話框形式展示
public void setView (View view)

因爲在寫的過程中AlertDialog.Builder也有show()的方法,誤以爲對話框不能利用自定義佈局中按鈕的監聽使其消失,查API發現AlertDialog.Builder的show()方法支持利用AlertDialog創建對話框並且show(),源代碼如下:

/**
  * Creates a {@link AlertDialog} with the arguments supplied to this builder and
  * {@link Dialog#show()}'s the dialog.
  */
        public AlertDialog show() {
            AlertDialog dialog = create();
            dialog.show();
            return dialog;
        }

所以AlertDialog.Builder可以show()出來卻不能消失,因爲它的show()方法是在內部創建的對話框

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章