什么是可重用对话框的最佳方法? [英] What's the best way to have reusable dialog boxes?

查看:249
本文介绍了什么是可重用对话框的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中创建可重用对话框的最佳方式是什么?

What's the best way to create reusable dialog boxes in Android?

阅读对话框开发指南,我知道我可以使用 AlertDialog.Builder(this); 我的活动 s,但如果我想在多个活动中使用它?如果这是另一个类,我会扩展它,所以 MyDialog扩展AlertDialog ,但是我不能使用 Builder

Reading through the Dialog dev guide, I know I can use AlertDialog.Builder(this); in one of my Activitys, but what if I want to use this in multiple Activities? If this was some other class I would extend it, so MyDialog extends AlertDialog, but then I cannot use the Builder.

任何建议?

推荐答案

code> AllMethod.java 并将该代码添加到该类文件中。

Crate one class file like as AllMethod.java and add this code in that class file.

public static void showAlert(Activity act, String msg, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder alert = new AlertDialog.Builder(act);
        alert.setMessage(msg);
        alert.setPositiveButton("OK", listener);
        alert.show();
    }

,您可以使用下面的代码中的任何类。

and you can use from any class like below code.

AllMethod.showAlert(mActivity, "", new DialogInterface.OnClickListener() {
        @Override
         public void onClick(DialogInterface dialog, int which) {
         // Do your code for click
         }
});

这篇关于什么是可重用对话框的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆