简体   繁体   English

如何为alertDialog Box创建一个公共类

[英]How to create a common class for alertDialog Box

How to create a class for alertDialogBox , and it should be called for every Activity . 如何为alertDialogBox创建一个类,应该为每个Activity调用它。 I used a close button on every Activity while pressing the close Button, The Alert Box should kill all activities in my application. 我按下关闭按钮时在每个Activity上都使用了一个关闭按钮,警报框应该杀死我的应用程序中的所有活动。

Create a class like CustomDialog.class , 创建一个类CustomDialog.class

and inside of it create a static method like the one below, 并在其中创建一个静态方法,如下所示,

 public static void ExitAppDialog(final Activity activity)
 {
     AlertDialog.Builder alertbox = new AlertDialog.Builder(activity);
     alertbox.setTitle("Warning");
     alertbox.setMessage("Exit Application?");
     alertbox.setPositiveButton("Yes", new
     DialogInterface.OnClickListener() {
     public void onClick(DialogInterface arg0, int arg1) {
         activity.finish();
     }
     });
     alertbox.setNegativeButton("No", new
     DialogInterface.OnClickListener() {
     public void onClick(DialogInterface arg0, int arg1) {

     }
     });
     alertbox.show();
 }

And in your activity's onBackPressed() call this method like this, 在你的活动的onBackPressed()中调用这个方法,

public void onBackPressed()
{
    CustomDialog.ExitAppDialog(myActivity.this );
}

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

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