我今天一整天都在寻找一些示例代码或教程,以便在完成任务时创建一个循环进度。完成此任务的时间会有所不同,并且有大量样本使用Thread.sleep(xxxx)使其循环,但这样效率不高。这是我想要做的,我想在单击按钮后加载使用JSON从Web服务填充的ListView。列表视图加载完全正常,但根据大小加载大约需要5-10秒,因此我想在用户等待时显示旋转的旋转。有人可以分享一些关于如何实现这一目标的示例代码吗?
谢谢
答案 0 :(得分:32)
new Load().execute();
致电。
class Load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog progDailog = new ProgressDialog(Activity.this);
progDailog.setMessage("Loading...");
progDailog.setIndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDailog.setCancelable(true);
progDailog.show();
}
@Override
protected String doInBackground(String... aurl) {
//do something while spinning circling show
return null;
}
@Override
protected void onPostExecute(String unused) {
super.onPostExecute(unused);
progDailog.dismiss();
}
}
答案 1 :(得分:11)
private class LoadAssync extends AsyncTask<String, Void, Void> {
protected void onPreExecute() {
ProgressDialog dialog;
dialog.setMessage("Loading...");
dialog.show();
}
protected Void doInBackground(final String... args) {
// you can do the code here
return null;
}
protected void onPostExecute(final Void unused) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
你可以像这样调用异步
LoadAssync mAsyync=new LoadAssync();
mAsyync.execute(null);
答案 2 :(得分:3)
您可以尝试以下代码,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}