如何在Android应用程序中以编程方式显示沙漏?
答案 0 :(得分:44)
您可以使用ProgressDialog
:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Thinking...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
上面的代码会在Activity
:
或者(或另外),您可以在Activity
的标题栏中显示进度指示器。
onCreate()
使用以下代码Activity
方法顶部附近need to request this as a feature:
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 <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();
}
}
}