简体   繁体   English

AlertDialog不起作用

[英]AlertDialog doesn't work

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case IDD_COLOR:
            return new AlertDialog(this); // The constructor AlertDialog(context) is not visible
    }

    return null;
}

Why? 为什么? What's wrong? 怎么了?

You cannot create an AlertDialog as it has a protected constructor, you can make AlertDialog 's by using AlertDialog.Builder . 您不能创建一个AlertDialog因为它有一个受保护的构造,可以使AlertDialog使用的AlertDialog.Builder

More information on that subject. 有关该主题的更多信息

The constructor AlertDialog(Context context) is protected , and is only visible to its class, sub-classes and classes within the same package. 构造函数AlertDialog(Context context) protected ,并且仅对其类,子类和同一包中的类可见。

See this link for how to create an AlertDialog : 请参阅此链接以了解如何创建AlertDialog

please use AlertDialog.Builder , like: 请使用AlertDialog.Builder ,例如:

AlertDialog.Builder builder = new AlertDialog.Builder(a)
        .setCustomTitle(buildAlertTitle(a, title, 18))
        .setMultiChoiceItems(choices, checkedChoices, multiChoiceClickListener)
        .setPositiveButton(okButtonLabel, okButtonClickListener)
        .setNegativeButton(cancelButtonLabel, cancelButtonClickListener);

AlertDialog alert = builder.create(); // create one

alert.show(); //display it 

For more information, please use Google "android AlertDialog.Builder sample" 有关更多信息,请使用Google“ android AlertDialog.Builder示例”
BR shawn BR肖恩

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

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