一个为什么要使用AlertDialog.Builder
类,而不是直接的方法可用于AlertDialog
, 例如,为什么要用AlertDialog.Builder.setCancellable
而非AlertDialog.setCancellable
?当然这是冗余的情况?AlertDialog VS AlertDialog.Builder
回答
因为AlertDialog.setCancellable
返回void且AlertDialog.Builder.setCancellable
返回AlertDialog.Builder
。
这意味着该构建器允许您链接一些设置,但少一些冗长。这只是一个方便的类
我认为工厂方法只是更方便。
AlertDialog允许您在活动/片段上方显示一个对话框。它通常用于提示用户进行交互,包括几个按钮或通知某事。
AlertDialog.Builder是内部静态类AlertDialog的,它允许您快速建立其方便的方法的对话框。它就像AlertDialog的助手类。它用于调用链中的方法。
但是谁帮忙?它的目的只是为了将所有内容简化成一个冗长的声明而不是多个单独的声明(设置标题等?)还是以某种方式提供更多功能? – stephenfin 2012-04-04 19:37:43
是的,它为了方便起见也方便调用链中的方法 – waqaslam 2012-04-04 19:38:49
@stephenfin实际上,no-builder类不仅仅是用于链接方法调用。他们往往像一个提供合理的默认值和定制选项的工厂。产品('AlertDialog')是不可变的/抽象的,而工厂同行则不是这样可以极大地简化创建产品的过程。以“StringBuilder”或“ProcessBuilder”为例。 – andr 2013-02-04 02:26:19
AlertDialog.Builder
做的属性,如setTitle()
或setMessage()
的设置,并不显示给用户。
AlertDialog
是一个显示其中已经在AlertDialog.Builder
组的那些属性。
这两个提到的地方的目的是,它允许设置与实际显示,从而使事情方便分开。
- 1. AlertDialog.Builder vs类扩展AlertDialog - 应用程序大小
- 2. AlertDialog VS Spinner vs ListView
- 3. AlertDialog show = new AlertDialog.Builder(this)未定义
- 4. AlertDialog.Builder打开另一个AlertDialog.Builder
- 5. 如何访问AlertDialog.Builder构建的AlertDialog消息?
- 6. AlertDialog内alertdialog安卓
- 7. AlertDialog.Builder错误
- 8. Android AlertDialog
- 9. AlertDialog.Builder - 分离到另一个类
- 10. AlertDialog onClickListener
- 11. 另一个alertdialog里面的AlertDialog
- 12. AlertDialog.Builder未定义
- 13. Android AlertDialog.Builder和softkeyboard
- 14. AlertDialog.Builder低于19
- 15. 在alertDialog
- 16. AlertDialog按钮打开新的AlertDialog
- 17. Android CountDownTimer和AlertDialog
- 18. AlertDialog问题
- 19. 自定义AlertDialog
- 20. Android隐藏AlertDialog.Builder(mycontext)
- 21. 如何取消AlertDialog.Builder
- 22. Android AlertDialog.Builder switch语句
- 23. AlertDialog不清单
- 24. 查找AlertDialog
- 25. 最大化AlertDialog?
- 26. 如何alertDialog
- 27. Alertdialog多个听众
- 28. AlertDialog到DialogFragment
- 29. AlertDialog.Builder访问特定行的问题
- 30. AlertDialog显示为空
所以它有点像'C++中的istream/ostream运算符?这听起来很合理 – stephenfin 2012-04-04 19:39:59