简体   繁体   English

来自另一个类的AlertDialog

[英]AlertDialog from another class

In mainActivity I have 在mainActivity中,我有

public void showDialog(String title, String message, String accept, String denied){
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setCancelable(true);

    builder.setPositiveButton(accept, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    builder.setNegativeButton(denied, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    builder.create().show();
}

It works inside main activity, but I am traing call this from another activity, so I wrote 它在主要活动中起作用,但是我正在从另一个活动中调用它,所以我写了

MainActivity o = new MainActivity();
o.showDialog("t", "t", "t", "t");

And app is crashing. 应用崩溃了。 I tried also wrote "this", "getAplicationContext()" instead of "MainActivity.this". 我试过也写了“ this”,“ getAplicationContext()”而不是“ MainActivity.this”。

It works inside main activity, but I am traing call this from another activity, so I wrote 它在主要活动中起作用,但是我正在从另一个活动中调用它,所以我写了

MainActivity o = new MainActivity(); MainActivity o =新的MainActivity(); o.showDialog("t", "t", "t", "t"); o.showDialog(“ t”,“ t”,“ t”,“ t”);

you shouldn't use the new operator on classes that extends Activity . 您不应该在扩展Activity类上使用new运算符。 Back to your problem, it looks to me like you want to minimize the repeating code. 回到您的问题,对我来说,您似乎想减少重复的代码。 One way, in your case could be an utility class, which contains your showDialog method. 在您的情况下,一种方法可能是实用程序类,其中包含您的showDialog方法。 Its signature would be slightly different because you need a Context object to instantiate the builder: 它的签名会稍有不同,因为您需要一个Context对象来实例化构建器:

public final class Utility { 

public static void showDialog(Context context, String title, String message, String accept, String denied){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setCancelable(true);

    builder.setPositiveButton(accept, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    builder.setNegativeButton(denied, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    builder.create().show();
}

 private Utility() {}
}

Note also that the class Utility, has a private default constructor , which means that you can't explicitly instantiate it. 还要注意,Utility类具有private default constructor ,这意味着您不能显式实例化它。

and the you can use like: 而您可以像这样使用:

  Utility.showDialog(ActivityName.class, "aa", "bb", "cc", "dd");

+1 for Blackbelt's answer. +1为Blackbelt的答案。

But just to follow up since OP wants to use it in another activity, you would use it in another activity like this. 但是,由于OP希望在其他活动中使用它,因此只是为了跟进,您可以在这样的其他活动中使用它。 Say you wanted to use it in an activity called SecondActivity : Utility.showDialog(SecondActivity.class, "aa", "bb", "cc", "dd"); 假设您想在名为SecondActivity的活动中使用它: Utility.showDialog(SecondActivity.class, "aa", "bb", "cc", "dd");

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

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