21
如何以編程方式在Android應用程序中顯示沙漏?android沙漏
如何以編程方式在Android應用程序中顯示沙漏?android沙漏
您可以使用ProgressDialog
:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Thinking...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
上面的代碼會顯示在您的Activity
的頂部下面的對話框:
或者(或另外),您可以顯示一個進度指標在您的Activity
的標題欄中。
您need to request this as a feature靠近你Activity
的onCreate()
方法使用下面的代碼的頂部:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
然後打開它這樣的:
setProgressBarIndeterminateVisibility(true);
,並把它像這樣關閉:
setProgressBarIndeterminateVisibility(false);
下面是使用的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 <Object,Void,String>{
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();
}
}
}
的問題是,顯示對話框後我跑了相對長的處理,其防止出現在末端的對話框的顯示治療時我不再需要! – Arutha 2010-01-26 16:00:54
看看'AsyncTask'。你可以在'onPreExecute()'和'onPostExecute'中顯示和隱藏'ProgressDialog',並在'doInBackground'中完成你的工作。 http://android-developers.blogspot.com/2009/05/painless-threading.html – 2010-01-26 16:26:10
也許值得一讀Android開發者指南「Designing For Responsiveness」http://developer.android.com/guide/practices/ design/responsiveness.html – 2010-01-26 17:02:11