内容简介:翻译自:https://stackoverflow.com/questions/7713222/android-progress-dialog
代码,完成后,将其显示在设备屏幕上.由于这需要大约3-4秒钟,因此屏幕保持黑色,我想使用进度对话框.这是我的代码:
package com.nextlogic.golfnews; // ALL THE IMPORTS .... public class Activity1 extends Activity { private ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); new Thread() { public void run() { try { sleep(2000); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag",e.getMessage()); } // dismiss the progressdialog progressDialog.dismiss(); } }.start();
该程序有效,但不再显示任何内容.我在logcat中有一个错误:
Only the original thread that created a view hierarchy can touch its views.
请你帮助我好吗 ?提前致谢.
内调用更新代码.
但是在你的情况下,我会使用 AsyncTask 来下载和更新进度条的进度.该任务具有在UI线程结束时运行的属性(因此您可以更新其中的视图,例如关闭进度对话框)
Here 是如何使用AsyncTask显示下载进度对话框的示例.
更新
Stackoverflow已经有你所有问题的答案. Here 是AsyncTask的示例,用于下载某些内容并显示下载进度.正是你想要的.
更新2
好的,这是使用AsyncTask的代码:
public class Activity1 extends Activity { private ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new AsyncTask<Integer, Integer, Boolean>() { ProgressDialog progressDialog; @Override protected void onPreExecute() { /* * This is executed on UI thread before doInBackground(). It is * the perfect place to show the progress dialog. */ progressDialog = ProgressDialog.show(Activity1.this, "", "Loading..."); } @Override protected Boolean doInBackground(Integer... params) { if (params == null) { return false; } try { /* * This is run on a background thread, so we can sleep here * or do whatever we want without blocking UI thread. A more * advanced use would download chunks of fixed size and call * publishProgress(); */ Thread.sleep(params[0]); // HERE I'VE PUT ALL THE FUNCTIONS THAT WORK FOR ME } catch (Exception e) { Log.e("tag", e.getMessage()); /* * The task failed */ return false; } /* * The task succeeded */ return true; } @Override protected void onPostExecute(Boolean result) { progressDialog.dismiss(); /* * Update here your view objects with content from download. It * is save to dismiss dialogs, update views, etc., since we are * working on UI thread. */ AlertDialog.Builder b = new AlertDialog.Builder(Activity1.this); b.setTitle(android.R.string.dialog_alert_title); if (result) { b.setMessage("Download succeeded"); } else { b.setMessage("Download failed"); } b.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dlg, int arg1) { dlg.dismiss(); } }); b.create().show(); } }.execute(2000); new Thread() { @Override public void run() { // dismiss the progressdialog progressDialog.dismiss(); } }.start(); } }
翻译自:https://stackoverflow.com/questions/7713222/android-progress-dialog
以上所述就是小编给大家介绍的《Android进度对话框》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 使用导航组件: 对话框目的地
- VBScript - 弹出“文件选择对话框”方法大全!
- jQuery实现定时隐藏对话框的方法分析
- 使用React手写一个对话框或模态框
- c# – Watin – 使用ConfirmDialogHandler处理确认对话框
- React造轮系列:对话框组件 - Dialog 思路
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Black Box Society
Frank Pasquale / Harvard University Press / 2015-1-5 / USD 35.00
Every day, corporations are connecting the dots about our personal behavior—silently scrutinizing clues left behind by our work habits and Internet use. The data compiled and portraits created are inc......一起来看看 《The Black Box Society》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
URL 编码/解码
URL 编码/解码