Android 单例对话框
我有一个处理大量进度对话框的 Android 应用程序。我必须为每个活动创建一个单独的对话框。
对话框创建在构造时将活动(上下文)作为参数。
有没有一种方法可以创建单个对话框(与应用程序而不是活动相关)并在不同的活动中显示它,这样我就不必重复创建它。
I have android application which deals with lot of progress dialogs.I have to create a separate dialog for each activity.
Dialog creation takes a activity(context) as parameter while constructing.
Is there a way by which I can create a single dialog (which is tied to application and not the activity) and show it in different activity so that I do not have to create it repeatedly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Utill 帮助器类中声明
showProgressDialog
和hideProgressDialog
,如以下代码片段所示,然后从需要显示 ProgressDialog 的活动中调用,例如在
onPreExecute() 方法,如下面的代码片段所示
,并使用以下代码隐藏 ProgressDialog
Declare
showProgressDialog
andhideProgressDialog
in Utill helper class as shown in following code snippetThen call from activity where you need to show the ProgressDialog for example in
onPreExecute()
method of AsyncTask class as shown in below code snippetand use following code to hide the progressDialog
将对话框代码放入辅助类的接收上下文的静态方法中可能是最好的方法。
Putting dialog code into a helper class's static method receiving a Context maybe the best way.
不幸的是,没有。您必须将对话框附加到活动,否则您的应用程序将容易崩溃。例如,您可能会遇到类似
android.view.WindowManager$BadTokenException
的异常。Unfortunately, no. You have to attach the dialog to an activity, otherwise your application will tend to crash. You could get exceptions like
android.view.WindowManager$BadTokenException
for instance.这并不是你问题的真正答案,但也许我的想法对你有帮助。我创建了一个 BaseActivity,有一个成员对话框、作为成员的活动上下文和两个方法,用于显示和隐藏进度对话框。所有其他活动都是从它延伸出来的。
It's not really the answer for your question, but maybe my idea helps you. I've created a BaseActivity, there is a member dialog, activity context as member and two methods, to show and to hide progess dialog. All other activities are extended from it.
如果您只想在对话框中定义 ProgressBar,请
在布局文件夹中定义此类和 XML
,并在 style.xml 中定义此样式
If you want ProgressBar only in the dialog define this class
and this XML in layout folder
and this style in style.xml