Android 如何创建可在整个活动中使用的AlertDialog

Android 如何创建可在整个活动中使用的AlertDialog,android,android-alertdialog,Android,Android Alertdialog,如何一劳永逸地构建AlertDialog。在整个活动中,我将在需要时显示它 您可以根据需要在任何Util类中创建方法- public static void showDialog(Context context, int msgResId) { if (context == null) return; new AlertDialog.Builder(context) .setMessage(msgResId)

如何一劳永逸地构建AlertDialog。在整个活动中,我将在需要时显示它

您可以根据需要在任何Util类中创建方法-

public static void showDialog(Context context, int msgResId) {
        if (context == null) return;
        new AlertDialog.Builder(context)
                .setMessage(msgResId)
                .create()
                .show();
    }
并在活动中随时拨打电话-

showDialog(MainActivity.this, R.string.your_string_res_id);
用于带有操作按钮的警报对话框-

在任何方法之外声明对话框-

private AlertDialog dialog;
您可以在活动的
onCreate()中创建对话框,如下所示-

dialog = new AlertDialog.Builder(MainActivity.this)
                .setMessage("Your message")
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                })
                .create();
 dialog.show();
无论什么时候你想展示,你都可以这样展示-

dialog = new AlertDialog.Builder(MainActivity.this)
                .setMessage("Your message")
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                })
                .create();
 dialog.show();

您可以在任何Util类中创建方法,如下所示:-

public static void showDialog(Context context, int msgResId) {
        if (context == null) return;
        new AlertDialog.Builder(context)
                .setMessage(msgResId)
                .create()
                .show();
    }
并在活动中随时拨打电话-

showDialog(MainActivity.this, R.string.your_string_res_id);
用于带有操作按钮的警报对话框-

在任何方法之外声明对话框-

private AlertDialog dialog;
您可以在活动的
onCreate()中创建对话框,如下所示-

dialog = new AlertDialog.Builder(MainActivity.this)
                .setMessage("Your message")
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                })
                .create();
 dialog.show();
无论什么时候你想展示,你都可以这样展示-

dialog = new AlertDialog.Builder(MainActivity.this)
                .setMessage("Your message")
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       //Your code
                })
                .create();
 dialog.show();
我使用这个类

您使用新的ModalDialog(此“示例”)

我希望这有帮助:)

在doModal()中,方法应该检查这一点

if (android.os.Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP) {
   msg.recycle ();
}
if(android.os.Build.VERSION.SDK_INT我使用这个类

您使用新的ModalDialog(此“示例”)

我希望这有帮助:)

在doModal()中,方法应该检查这一点

if (android.os.Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP) {
   msg.recycle ();
}

if除非您想强制用户确认警报消息,否则用户需要采取负面和积极的按钮采取行动。除非您想强制用户确认警报消息,否则请考虑使用烤面包机。是的,用户需要对N采取行动。负按钮和正按钮。我将在util类中配置有关对话框的所有内容,这包括负按钮和正按钮。我如何在MainActivity中显示它。谢谢。这解决了它。我将在util类中配置有关对话框的所有内容,这包括负按钮和正按钮。我如何在MainActivity中显示它。而好的,这就解决了。