簡體   English   中英

Android文檔與Android Studio警告不同

[英]Android documentation differs from Android Studio warning

我目前正在實現其中有一個CheckBoxAlertDialog 為了做到這一點,我給一個包含CheckBox的視圖充氣,然后使用setView(View)方法將其添加到Dialog中,但是通過這種方式,我得到了很大的填充。 根據我的閱讀,解決該問題的方法顯然是使用setView(View, int, int, int, int)方法(設置所有方向的填充),但是當我嘗試實現android studio時給我一個錯誤(可以通過添加@SuppressLint("RestrictedApi")來避免@SuppressLint("RestrictedApi")但我不知道該怎么做也不意味着什么,所以如果您知道不要猶豫向我解釋),然后說該方法已被棄用,很奇怪的是,在文檔中( 在此處 ),沒有什么可說不贊成使用此方法的,所以我想知道,在這種情況下,我應該信任文檔還是androidStudio? (也是使用第二種方法的一個好主意)

這是AlertDialog的代碼:

@SuppressLint("RestrictedApi")
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    View checkBoxView = View.inflate(getContext(), R.layout.checkbox, null);
    CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox_dontaskmeagain);

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            SharedPrefUtils.setShowAlertDialog(getContext(), b);
        }
    });

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.alert_everyday_dialog_message);
    builder.setTitle(R.string.alert_dialog_title)
            .setView(checkBoxView, 0, 0, 0, 0) //this apparently is deprecated
            .setPositiveButton(R.string.alert_dialog_delete, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onEverydayDialogPositiveClick(DeleteTaskEverydayDialog.this);
                }
            })
            .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onEverydayDialogNegativeClick(DeleteTaskEverydayDialog.this);
                }
            });
    return builder.create();
}

請注意,這是一個自AlertDialog擴展的自定義類,其中還有一些其他功能,但我認為不需要它們,如果您認為有必要,請告訴我,我將提供它們

您正在查看錯誤的文檔-即AlertDialog 您正在調用的代碼位於AlertDialog.Builder 它沒有任何文檔,因為它帶有@hide注釋,因此不包含在SDK及其文檔中。 支持庫源中所示,它還具有@RestrictTo@Deprecated批注。

javadoc中給出了一些隱藏的基本原理:

這是當前隱藏的,因為似乎人們應該可以在視圖周圍放置填充。

一般來說,您不應調用隱藏或受限方法。 尋找其他方式來實現您的目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM