Android:ProgressDialog 不显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2798443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: ProgressDialog doesn't show
提问by Select0r
I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail.
我正在尝试为 Android 应用程序创建一个 ProgressDialog(只是一个简单的向用户展示正在发生的事情,没有按钮或任何东西),但我无法正确完成。我已经浏览了论坛和教程以及 SDK 附带的示例代码,但无济于事。
This is what I got:
这是我得到的:
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
(...)
ProgressDialog pd = new ProgressDialog(MyApp.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Working...");
pd.setIndeterminate(true);
pd.setCancelable(false);
// now fetch the results
(...long time calculations here...)
// remove progress dialog
pd.dismiss();
I've also tried adding pd.show();
and messed around with the parameter in new ProgressDialog
resulting in nothing at all (except errors that the chosen parameter won't work), meaning: the ProgressDialog won't ever show up. The app just keeps running as if I never added the dialog.
我还尝试添加pd.show();
参数并弄乱参数,new ProgressDialog
结果什么也没有(除了所选参数不起作用的错误),这意味着:ProgressDialog 永远不会出现。该应用程序一直在运行,就好像我从未添加过对话框一样。
I don't know if I'm creating the dialog at the right place, I moved it around a bit but that, too, didnt't help. Maybe I'm in the wrong context? The above code is inside private ViewGroup _createInputForm()
in MyApp
.
我不知道我是否在正确的位置创建了对话框,我稍微移动了它,但这也无济于事。也许我在错误的上下文中?上面的代码private ViewGroup _createInputForm()
在MyApp
.
Any hint is appreciated,
任何提示表示赞赏,
回答by RoflcoptrException
you have to call pd.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.
您必须在长计算开始之前调用 pd.show,然后计算必须在单独的线程中运行。该线程完成后,您必须立即调用 pd.dismiss() 关闭进程对话框。
here you can see an example:
在这里你可以看到一个例子:
the progressdialog is created and displayed and a thread is called to run a heavy calculation:
创建并显示进度对话框,并调用一个线程来运行繁重的计算:
@Override
public void onClick(View v) {
pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
Search search = new Search( ... );
SearchThread searchThread = new SearchThread(search);
searchThread.start();
}
and here the thread:
这里是线程:
private class SearchThread extends Thread {
private Search search;
public SearchThread(Search search) {
this.search = search;
}
@Override
public void run() {
search.search();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
displaySearchResults(search);
pd.dismiss();
}
};
}
回答by Select0r
I am giving you a solution for it,
try this...
First define the Progress Dialog in the Activity before onCreate()
method
我给你一个解决方案,试试这个...首先在Activity中定义Progress Dialog beforeonCreate()
方法
private ProgressDialog progressDialog;
Now in the onCreate
method you might have the Any button click on which you will change the Activity on any action. Just set the Progress Bar there.
现在在该onCreate
方法中,您可能会单击 Any 按钮,您将在该按钮上更改任何操作的 Activity。只需在那里设置进度条。
progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");
Now use thread to handle the Progress Bar to Display and hide
现在使用线程来处理要显示和隐藏的进度条
new Thread()
{
public void run()
{
try
{
sleep(1500);
// do the background process or any work that takes time to see progress dialog
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progress dialog
progressDialog.dismiss();
}
}.start();
That is all!
就这些!
回答by lory105
回答by Peter Stuifzand
This is also possible by using AsyncTask. This class creates a thread for you. You should subclass it and fill in the doInBackground(...)
method.
这也可以通过使用AsyncTask 来实现。这个类为你创建一个线程。您应该将其子类化并填写doInBackground(...)
方法。