谁能解释一下在 android 应用程序开发中创建 AlertDialog 的更好解决方案是什么?

Can anyone explain me what is better solution for creating an AlertDialog in the android app development?

在 android 应用程序开发中创建 AlertDialog 的更好解决方案是什么:

  1. 正在创建带有 AlertDialog.Builder 的 AlertDialog 片段(class 扩展 对话框片段)
  2. 创建带有 AlertDialog.Builder 且没有片段的 AlertDialog

视情况而定。

更简单的解决方案是不扩展 DialogFragment 的解决方案。我将它用于具有一些文本和两个按钮 (yes/no) 的简单对话框。

DialogFragment 变体是完全可自定义的,具有 Fragment 的所有功能,尤其是使用您自己的自定义布局时。

视情况而定,

为了简单起见,我总是像这样创建一个标准的 AlertDialog:

   AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       builder = new AlertDialog.Builder(this, R.style.AlertDialogStyle);
    } else {
       builder = new AlertDialog.Builder(this);
    }
    builder.setMessage(getString(R.string.message))
    .setPositiveButton(getString(R.string.ready), new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
         }
    })

.setIcon(android.R.drawable.ic_dialog_alert)
.setCancelable(true)
.show();

但如果您想在对话框中构建更多功能,例如添加列表和执行自定义点击功能,您可以extend DialogFragment class

希望对您有所帮助