如何调用通用活动完成

发布于 2024-12-07 08:28:20 字数 999 浏览 0 评论 0原文

我有一个公共类,它具有一些常见的通用功能,例如用于在我的应用程序中显示对话框。我创建了一个通用的 alertButtonDialog 函数,并希望在使用对话框时在活动中调用它。我对 Java 很陌生,所以如果它非常基础,请原谅。

public static class AlertDialogs{

    public static void alertButtonDialog(Activity activity, Context context, String title, String message, 
            String positiveButton, String negativeButton) {

        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setTitle(title);
        alertBuilder.setMessage(message);
        alertBuilder.setPositiveButton(positiveButton, new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Activity.this.finish(); // *?? How to do this part ??*
                // the activity to be finished is the activity which calls this function
            }
        })

    }
}

稍后在任何其他活动中,每当我显示对话框时,我都会这样做

 AlertDialogs.alertButtonDialog(...all my Strings...)

这只是为了方便访问。

I have a public class which has some common generic functions, like e.g. for displaying dialogs in my application. I made a generic alertButtonDialog function and want to call it in activities whenever I'm using dialogs. I am very new to Java so please excuse me if it's very basic.

public static class AlertDialogs{

    public static void alertButtonDialog(Activity activity, Context context, String title, String message, 
            String positiveButton, String negativeButton) {

        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setTitle(title);
        alertBuilder.setMessage(message);
        alertBuilder.setPositiveButton(positiveButton, new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Activity.this.finish(); // *?? How to do this part ??*
                // the activity to be finished is the activity which calls this function
            }
        })

    }
}

Later in any other activity, whenever I'm displaying a dialog, I would just do

 AlertDialogs.alertButtonDialog(...all my Strings...)

This is only for convenience accessing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2024-12-14 08:28:20

alertButtonDialog 函数中,将 Activity Activity 参数定义为最终参数。然后,从 onClick 侦听器中调用 activity.finish();

public static void alertButtonDialog(final Activity activity, Context context, String title, String message, 
        String positiveButton, String negativeButton) {

    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
    ...
    alertBuilder.setPositiveButton(positiveButton, new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            activity.finish();
        }
    })

}

请注意,您不需要同时使用 ActivityContext - 因为前者扩展了后者 - 除非您希望将 Activity 作为 null 传递到某处。

In your alertButtonDialog function, define the Activity activity argument as final. Then, from within the onClick listener invoke activity.finish();.

public static void alertButtonDialog(final Activity activity, Context context, String title, String message, 
        String positiveButton, String negativeButton) {

    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
    ...
    alertBuilder.setPositiveButton(positiveButton, new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            activity.finish();
        }
    })

}

Note that you do not need both an Activity and a Context -since the former extends the later-, unless you are expecting to pass activity as null somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文