单击按钮时显示进度对话框
我在我的应用程序中使用以下代码。
Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog pd = new ProgressDialog(v.getContext());
pd.setTitle("Please wait.......");
pd.show();
// some task which will take minimum 2 or 3 seconds
// e.g. parsing XML file
pd.dismiss();
}
});
我认为根据上面的代码,当我单击按钮时,进度对话框必须显示在屏幕上,但它没有显示。为什么我不知道。
但是,如果我删除 pd.dismiss()
它的显示,那么也仅在按钮释放后。
如果我也将任何无限循环代替我的任务,它不显示进度对话框。
我的代码正确吗?如果有人知道我的要求的解决方案,请回复此帖子。
谢谢。
I am using the below code in my application.
Button button = new Button(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog pd = new ProgressDialog(v.getContext());
pd.setTitle("Please wait.......");
pd.show();
// some task which will take minimum 2 or 3 seconds
// e.g. parsing XML file
pd.dismiss();
}
});
I thought according to above code, when i click the button the progress dialog has to be displayed on screen, but its not displaying. Why i don't know.
But if i remove pd.dismiss()
its displaying, that also after button released only.
If i put any infinite loop in place of my task also, its not displaying progress dialog.
Is my code correct ? If anybody knows solution to my requirement please reply to this post.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AsyncTasks 就是为此目的而设计的。单击按钮时启动 asyncTask,并在 asyncTask 的 preExecute 中显示对话框,并在 asyncTask 的 PostExecute() 中关闭该对话框。在background()方法中执行需要时间的活动。
AsyncTasks are designed for this purpose. start an asyncTask when the button is clicked, and in preExecute of the asyncTask show the dialog and on PostExecute() of the asyncTask dismiss the dialog. Do the activity which takes time in the background() method.
使用 AsychTask 类只需将代码写入 inBackground() 方法,并使用 onUpdateProgress() 显示进度并在 onPostExecute() 上关闭,
这里是示例链接
http://developer.android.com/reference/android/os/AsyncTask.html
http://www.vogella.de/articles/AndroidPerformance/article.html
use the AsychTask class for just write your code into the inBackground() method and show the progress using onUpdateProgress() and dismiss on onPostExecute()
here is the example links
http://developer.android.com/reference/android/os/AsyncTask.html
http://www.vogella.de/articles/AndroidPerformance/article.html