简体   繁体   English

Android文档与Android Studio警告不同

[英]Android documentation differs from Android Studio warning

I'm currently implementing an AlertDialog in which there is a CheckBox . 我目前正在实现其中有一个CheckBoxAlertDialog In order to do that, I inflate a view containing a CheckBox and then I add it to my Dialog using the setView(View) method, but by proceeding that way I get huge padding. 为了做到这一点,我给一个包含CheckBox的视图充气,然后使用setView(View)方法将其添加到Dialog中,但是通过这种方式,我得到了很大的填充。 From what I read, a solution to that problem would apparently be to use the setView(View, int, int, int, int) method (which sets the padding for all direction) instead, but when I try to implement that android studio gives me an error (that can be avoided by adding @SuppressLint("RestrictedApi") but I don't know what that does nor means, so if you know don't hesitate to explain to me) and then says that this method is deprecated, the weird thing is that in the documentation (right here ), there is nothing saying that this method is deprecated, so I would like to know, in these kinds of moment, should I trust the documentation or androidStudio? 根据我的阅读,解决该问题的方法显然是使用setView(View, int, int, int, int)方法(设置所有方向的填充),但是当我尝试实现android studio时给我一个错误(可以通过添加@SuppressLint("RestrictedApi")来避免@SuppressLint("RestrictedApi")但我不知道该怎么做也不意味着什么,所以如果您知道不要犹豫向我解释),然后说该方法已被弃用,很奇怪的是,在文档中( 在此处 ),没有什么可说不赞成使用此方法的,所以我想知道,在这种情况下,我应该信任文档还是androidStudio? (and also if it is a good idea to use the second method) (也是使用第二种方法的一个好主意)

here is the code of the AlertDialog : 这是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();
}

Note that this is a custom class that extends from AlertDialog in which there are some other functions but I don't think that they are needed, if you think so, tell me and I'll provide them 请注意,这是一个自AlertDialog扩展的自定义类,其中还有一些其他功能,但我认为不需要它们,如果您认为有必要,请告诉我,我将提供它们

You're looking at the wrong docs - namely AlertDialog . 您正在查看错误的文档-即AlertDialog The code you are calling is on AlertDialog.Builder . 您正在调用的代码位于AlertDialog.Builder It does not have any docs since it is annotated with @hide and thus excluded from SDK and its documentation. 它没有任何文档,因为它带有@hide注释,因此不包含在SDK及其文档中。 It also has @RestrictTo and @Deprecated annotations as seen in the support library source . 支持库源中所示,它还具有@RestrictTo@Deprecated批注。

Some rationale for hiding is given in the javadoc: javadoc中给出了一些隐藏的基本原理:

This is currently hidden because it seems like people should just be able to put padding around the view. 这是当前隐藏的,因为似乎人们应该可以在视图周围放置填充。

Generally speaking, you should not be calling hidden or restricted methods. 一般来说,您不应调用隐藏或受限方法。 Find some other way to achieve your goal. 寻找其他方式来实现您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM