在android应用程序开发中创建AlertDialog的更好解决方案是什么:
- 在片段(类扩展对话框片段(
- 使用AlertDialog.Builder创建不带片段的AlertDialog
这取决于
为了简单起见,我总是创建一个像这样的标准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
希望它能帮助
取决于。
更简单的解决方案是不扩展CCD_ 2的解决方案。我把它用于简单的对话框,其中有一些文本和两个按钮(是/否(。
DialogFragment
变体是完全可定制的,具有Fragment
的所有功能,尤其是使用您自己的自定义布局。