我应该使用什么情况下AlertDialog.Builder吗? [英] what context should i use AlertDialog.Builder in?

查看:129
本文介绍了我应该使用什么情况下AlertDialog.Builder吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下我应该使用什么背景下AlertDialog.Builder类?我是新的Andr​​oid应用程序的开发和老​​实说,我不知道要使用的上下文时?

Could anyone please explain what context should i use the AlertDialog.Builder class? I am new to android app development and I frankly don't understand which context to use when?

说,我想创建AlertDialog.Builder类的对象 -

Say, I want to create an object for AlertDialog.Builder class -

AlertDialog.Builder ab = new AlertDialog.Builder();
ab.setMessage("Test");

ab.show();

我应该使用它在什么情况下?是否有所不同,如果我使用的警告对话框的onCreate OnClickListener 或任何此类事件的处理程序?

What context should I use it in? Does it differ if I use the Alert Dialog onCreate or OnClickListener or in the handler of any such event?

推荐答案

在我的应用程序的第一个版本我做不使用onCreateDialog的错,而是建立并显示对话框自己。如果你自己做,你要照顾的东西像关闭对话框前的活动结束()编否则窗口会泄漏。

In the first version of my app I made the mistake of not using onCreateDialog and instead built and showed the dialogs myself. If you do it yourself you have to take care of things like dismissing the dialog before the activity is finish()ed otherwise a window will leak.

我会覆盖onCreateDialog在您的活动并返回ab.create()(未示出())。那么onCreateDialog将处理显示对话框,你只需要调用的ShowDialog(ID)。

I would override onCreateDialog in your activity and return ab.create() (not show()). onCreateDialog will then handle showing the dialog and you'll just have to call showDialog(id).

这篇关于我应该使用什么情况下AlertDialog.Builder吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆