简体   繁体   English

如何创建全局警报对话框

[英]How to create a global alert dialog

I want to make a "global" alert dialog for some of my activities.我想为我的一些活动制作一个“全局”警报对话框。 What i mean is that i created a class that extends Activity also created a function for the alert dialog that takes two parameters (message and title).我的意思是我创建了一个扩展Activity的类,还为带有两个参数(消息和标题)的警报对话框创建了一个函数。

public class MyDialogAlert extends Activity {

public void createDialog(String title, String message)
{
    new AlertDialog.Builder(this)
    .setTitle(title)
    .setMessage(message)
    .setPositiveButton("yes", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //(e.g) open another activity
        }
    })
    .setNegativeButton("no", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //some stuff ..
        }
    })
    .show();
}

} }

How can i call this function in different activities and change it`s negative and positive buttons to do other operations than those defined initially in the above class?我如何在不同的活动中调用此函数并更改它的负按钮和正按钮以执行上述类中最初定义的操作之外的其他操作?

Thanks for the help :)谢谢您的帮助 :)

You can create sperate class that have static method for showing dialog on any activity您可以创建具有静态方法的单独类,用于在任何活动上显示对话框

public class DialogCaller {

public static void showDialog(Context context,String title,String message, 
        DialogInterface.OnClickListener onClickListener) {

    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setPositiveButton("Ok",onClickListener);
    dialog.setNegativeButton("Cancel",null);
    dialog.show();
}
}

And calling the method will be like this并且调用方法会是这样

   DialogCaller.showDialog(this,"title","message",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

According to FinalDark's working answer and Faisal Qayyum question, i hope it helps someone on how to dismiss the dialog and adding negative button listener根据FinalDark 的工作答案和 Faisal Qayyum 问题,我希望它可以帮助某人了解如何关闭对话框并添加否定按钮侦听器

In the calling method, add switch case:在调用方法中,添加switch case:

DialogCaller.showDialog(this,"title","message",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
           switch (option) {
             case DialogInterface.BUTTON_POSITIVE:
                  // Add your code if it positive button
                  break;

             case DialogInterface.BUTTON_NEGATIVE:
                  // Add your code if it negative button
                  break;
          }
        }
    });

And if you want to add listener when the negative button clicked, just change null to the listener:如果您想在点击否定按钮时添加侦听器,只需将null更改为侦听器:

dialog.setNegativeButton("Cancel",onClickListener);

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

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