Android:ProgressDialog 不显示 [英] Android: ProgressDialog doesn't show
问题描述
我正在尝试为 Android 应用创建一个 ProgressDialog(只是一个简单的向用户展示正在发生的事情,没有按钮或任何东西),但我无法正确完成.我已经浏览了论坛和教程以及 SDK 附带的示例代码,但无济于事.
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.
这是我得到的:
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();
我也尝试添加 pd.show();
并在 new ProgressDialog
中处理参数导致什么都没有(除了所选参数的错误行不通),意思是:ProgressDialog 永远不会出现.该应用程序一直在运行,就好像我从未添加过对话框一样.
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.
我不知道我是否在正确的位置创建了对话框,我稍微移动了它,但这也无济于事.也许我在错误的上下文中?上面的代码在MyApp
的private ViewGroup _createInputForm()
里面.
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
.
感谢任何提示,
推荐答案
您必须在长计算开始之前调用 pd.show,然后计算必须在单独的线程中运行.该线程完成后,您必须立即调用 pd.dismiss() 关闭进程对话框.
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.
在这里你可以看到一个例子:
here you can see an example:
progressdialog 被创建并显示出来,并调用一个线程来运行一个繁重的计算:
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();
}
这里是线程:
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();
}
};
}
这篇关于Android:ProgressDialog 不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!