【问题标题】:how to reuse android alertdialog如何重用android alertdialog
【发布时间】:2016-11-06 17:56:41
【问题描述】:

我想重用 alertDialog 的代码并将其作为函数调用放入另一个 java 文件中。但是“this”不能用来代替“MyActivity.this”吗?如何将其作为参数传递?最好是通用代码。

    AlertDialog alertDialog = new AlertDialog.Builder(MyActivity.this).create();
            alertDialog.setTitle("Alert");
            alertDialog.setMessage("Alert message to be shown");
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
            alertDialog.show();

【问题讨论】:

  • 这是一个上下文,你可以发送一个上下文来复用它:new AlertDialog.Builder(context).create()
  • 为什么我不能使用 getapplicationContext() 因为它比使用 MyActivity.this 更通用,必须在每个 java/project 文件中进行更改。

标签: android this android-alertdialog code-reuse


【解决方案1】:

您可以在一个类中单独编写警报对话框代码,如下所示:

public class Utils{
  public static void showMessage(final Activity activity, String title, String posText){

    MaterialDialog dialog = new MaterialDialog.Builder(activity)
            .content(title)
            .positiveText(posText)
            .onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                    dialog.dismiss();
                }
            })
            .build();
    dialog.show();

}
}

从您的活动/片段中,您可以将其称为:

活动,

AppUtils.showMessage(this, getString("your text"), getString("your text"));

片段,

AppUtils.showMessage(getActivity(),getString("your text"), getString("your text"));

在片段或活动中导入 Utils 类,然后一切都会正常工作。

希望这可以帮助您解决问题。

【讨论】:

  • 你能测试运行代码吗?从活动中传递“this”会出错
  • @DriveCarefully 我在我的应用程序中使用了相同的代码.. 它工作正常。如果它给你带来问题,你可以尝试 'youractivity.this' 而不是这个。
  • 由于代码包含其他项目,例如 MaterialDialog,所以我无法对其进行一些测试。但似乎我们可以传递 Activity 活动而不是 Context 上下文。有什么区别吗?
【解决方案2】:
public class Utils {
    public void showDialog(Context context) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        alertDialog.show();
    }
}

在您的Activity 中,您可以重复使用显示对话框

new Utils().showDialog(MyActivity.this);

【讨论】:

  • 只为一个实用程序初始化一个类不是一个好主意。只需将您的 showDialog 设为static
  • @Enzokie 你能解释一下为什么吗?或者你能给我一些有用的链接吗
  • Thus, you should avoid creating object instances you don't need to, 其次,如果您可以将 showDIalog 设为静态;你可以简单地这样做Utils.showDialog(....) 和它的方式更清洁。
【解决方案3】:

您可以在单独的类中使用类似的东西,例如我使用了AlertUtils.java

public class AlertUtils
{
    public static void showOKDialog(Context context, String title, String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, null);
        builder.show();
    }
}

在这个方法中,你传递的Context可能是你Activity的this,例如:MyActivity.this或者片段的getContext()

AlertUtils.showOKDialog(MyActivity.this, "title of dialog", "message to display in dialog");

【讨论】:

  • 为什么我不能使用 getapplicationContext() 因为它比使用 MyActivity.this 更通用,必须在每个 java/项目文件中更改
  • 这是一个更容易接受的解决方案,只是我必须完全重构活动名称。代码足够通用。
  • 这里对不同类型的上下文及其可以提供的内容进行了很好的解释:Context Compatibilities 向下滚动到接近一半的上下文兼容性部分。基本上,applicationContext 没有显示对话框的能力。 Activity 上下文是唯一可以显示对话框的上下文。
猜你喜欢
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 2013-07-05
相关资源
最近更新 更多