7

AlertDialog 的构建器类的 .create() 和 .show() 方法有什么区别吗?就像我们使用以下命令创建警报对话框一样:

AlertDialog.Builder builder = new 
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("");
builder.setPositiveButton(....)
builder.setNegativeButton(....)

推荐使用什么练习,为什么?

builder.create() //I have seen this creates and displays the dialog

或者

builder.show() //this also displays the dialog

或者

builder.create().show() //well same thing

我已阅读文档。但无法从中理解任何意义。有任何想法吗 ?

4

2 回答 2

5

obj.create()-For create Dialog

obj.show()-For show Dialog <- 没有它,如果你创建了,你就不能显示对话框。

obj.create().show()-create and show Dialog i mean both same as above two in one statement.

于 2012-05-23T10:54:03.870 回答
0

builder.show()返回一个 AlertDialog 对象并立即显示它。如文档中所述,调用此方法在功能上等同于:

AlertDialog dialog = builder.create();
dialog.show();

builder.create() returns an AlertDialog object with the arguments supplied to the builder, without showing it. This might be useful if you want to create and store the AlertDialog object for additional processing, after which dialog.show() may be called.

于 2019-10-16T11:35:50.590 回答