android - 拥有可重用对话框的最佳方式是什么?

标签 android dialog android-alertdialog

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

通读 Dialog dev guide ,我知道我可以在我的一个 Activity 中使用 AlertDialog.Builder(this);,但是如果我想在多个 Activity 中使用它怎么办?如果这是其他一些类,我会扩展它,所以 MyDialog extends AlertDialog,但是我不能使用 Builder

有什么建议吗?

最佳答案

创建一个类文件,如 AllMethod.java 并将此代码添加到该类文件中。

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();
    }

你可以像下面的代码一样使用任何类。

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

关于android - 拥有可重用对话框的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026571/

相关文章:

android - 当 Dialog 遇上 videoview

android - 如何对话框在外部触摸时保持打开状态

java - SWT - 需要有关文本框/表格/添加和删除值的建议

Android:如何自定义 AlertDialog、Dialog 和 Spinner 标题

android - 在android中将透明背景设置为alertdialog

android - 检查帐户是否已在 AccountManager 中注册

android - 如何在另一个类中委托(delegate)一个字符串而不重新定义它?

java - 将数据从 Activity 实时传递到子 Activity

android - FCM : onNewToken vs FirebaseInstanceId

javascript - jQuery 和 <dialog> 元素