我有以下几行代码:
1) m_ProgressDialog = ProgressDialog.show(m_Context, "", m_Context.getString(R.string.dictionary_loading));
2) //important code stuff: interact with db, change some textview values (= 2-3 seconds if i'm unlucky)
3) m_ProgressDialog.dismiss();但发生的情况是,阶段2)发生在1)之前。这是错误的。首先UI冻结,然后出现对话框。
阶段2)是一些与数据库交互的代码,可能还会更改一些textViews..but,因为这可能需要一段时间,所以我决定显示进度对话框,以便用户知道正在进行真正重要的事情。我不能使用Async来做这些操作,因为UI代码和db代码都是mengled的,这只会让我的生活变得复杂。
如何在请求时强制显示对话框??..在我看来,代码只是将其添加到“当我有空闲时间时&我现在没有时间”堆栈中。
发布于 2012-08-02 15:28:41
您正在UI线程上执行您的工作。你应该使用一个单独的线程来保持UI (进度条)的响应。看看AsynchTask吧。
发布于 2012-08-02 15:37:14
不要使用UiThread进行后台操作,这会导致screen.You的冻结,必须使用单独的线程,如异步任务。
如下图所示
在……里面
onCreate()
{
dialog.show();
new DownloadFilesTask().excute()
}
class DownloadFilesTask extends AsyncTask<Void,Void,Void>
{
protected Long doInBackground(URL... urls) {
//Background operation
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//Update you Ui here
dialog.dismiss();
}
});
}
}发布于 2021-07-06 17:37:15
在大多数情况下,如果您只想拥有两个方法,ShowLoading()和HideLoading()只需使用以下代码
public static void ShowLoading()
{
HideLoading();
myLoadingThread = new Thread(new ThreadStart(LoadingThread));
myLoadingThread.Start();
}
private static void LoadingThread()
{
Looper.Prepare();
myProgressDialog = new ProgressDialog(myActivity,
Resource.Style.AppTheme_Dialog);
myProgressDialog.SetMessage("Loading..."); // Or a @string...
myProgressDialog.SetIcon(Resource.Drawable.your_loading_icon);
myProgressDialog.SetProgressStyle(ProgressDialogStyle.Spinner);
myProgressDialog.SetCancelable(false);
myProgressDialog.Show();
Looper.Loop();
}
public static void HideLoading()
{
if (myProgressDialog != null)
{
myProgressDialog.Dismiss();
myProgressDialog = null;
}
if (myLoadingThread != null)
myLoadingThread.Abort();
}现在我声明并解释我在代码示例中使用的以下变量,其中一个是全局变量,是的,如果您不喜欢使用全局变量,或者您希望一次加载两个对话框(wtf...)寻找另一种解决方案。这是最简单的方式,最友好,没有奇怪的代码,嵌套的方法,新的类和继承到处都是这样简单的事情:
private Thread myLoadingThread;
private ProgressDialog myProgressDialog;
// Some people will hate me for this, but just remember
// to call myActivity = this; on each OnStart() of your app
// and end with all your headaches
public Activity myActivity; https://stackoverflow.com/questions/11772815
复制相似问题