一个AlertDialog,用于多活动?

时间:2011-04-27 00:50:39

标签: android alertdialog

我有一个需要在多个活动中使用的AlertDialog。如何通过自己的类实现这一目标?谢谢你的帮助!〜!

我尝试了以下内容并在活动中访问它:

Alerts.sdCardMissing();

我试图创建的课程:

public class Alerts {

public static void sdCardMissing() {
    AlertDialog alertDialog = new AlertDialog.Builder(null).create();
    alertDialog.setTitle("External Storage State");
    alertDialog
            .setMessage("Your SD-Card is not mounted!  If the device is plugged into a computer via the USB, please disconect the device.");
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // this.finish();
        }
    });
    // alertDialog.setIcon(R.drawable.icon);
    alertDialog.show();
}

}

1 个答案:

答案 0 :(得分:2)

...试

public class Alerts {
    public static void sdCardMissing(Context context) {
        // Pass context to AlertDialog.Builder
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        ...
    }
}

然后从活动中调用它......

// Pass the Activity context as 'this'
Alerts.sdCardMissing(this);