2 个答案:
答案 0 :(得分:44)
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Thinking...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
上面的代码会在Activity:之上显示以下对话框
或者(或另外),您可以在Activity的标题栏中显示进度指示器。
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
然后像这样打开它:
setProgressBarIndeterminateVisibility(true);
然后将其关闭:
setProgressBarIndeterminateVisibility(false);
答案 1 :(得分:3)
以下是使用AsyncTask执行此操作的简单示例:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
...
new MyLoadTask(this).execute(); //If you have parameters you can pass them inside execute method
}
private class MyLoadTask extends AsyncTask {
private ProgressDialog dialog;
public MyLoadTask(MyActivity act) {
dialog = new ProgressDialog(act);
}
protected void onPreExecute() {
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected String doInBackground(Object... params) {
//Perform your task here....
//Return value ... you can return any Object, I used String in this case
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return(new String("test"));
}
@Override
protected void onPostExecute(String str) {
//Update your UI here.... Get value from doInBackground ....
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}