安卓的AlertDialog可以设置View和ViewGroup到AlertDialog中。
所以任务进度就可以显示到对话框里面,而且对话框是非阻塞模式的。相对PopupWindows来说。
运行任务中,要显示后台进度对话框,界面如下:
自定义一个类,管理和显示后台任务进度对话框。
package com.xx.splashdemo;
import java.util.ArrayList;
import java.util.List;
import com.xx.adapterhelper.CommonAdapter;
import com.xx.adapterhelper.ViewHolder;
import com.xx.utils.ScreenUtils;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ShowTaskProgressDialog {
private TextView txtTaskPercentInfo;
private TextView txtDoTaskName;
private ProgressBar prgTask;
private String mTitle = "消息";
private ListView listView;
private List<String> mDatas = new ArrayList<String>();
private CommonAdapter<String> mAdapter;
private View mView;
private Context mContext;
protected ShowTaskProgressDialog(Context context) {
mContext = context;
//setContentView(R.layout.popup_doing_task);
mView = LayoutInflater.from(mContext).inflate(R.layout.popup_doing_task, null);
txtDoTaskName = (TextView) mView.findViewById(R.id.txtDoTaskName);
txtTaskPercentInfo = (TextView) mView.findViewById(R.id.txtTaskPercentInfo);
prgTask = (ProgressBar) mView.findViewById(R.id.prgTask);
prgTask.setMax(100);
listView = (ListView) mView.findViewById(R.id.lvMsg);
mAdapter = new CommonAdapter<String>(mContext.getApplicationContext(), mDatas, R.layout.simple_item) {
@Override
public void convert(ViewHolder holder, String item) {
holder.setText(R.id.txtItemName, item);
}
};
listView.setAdapter(mAdapter);
//--設置自動滾動到底部
listView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
//super.setButton(DialogInterface.BUTTON_POSITIVE, "確定", null);
}
public void show() {
AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle(mTitle).setPositiveButton("確定", null).create();
alertDialog.setCanceledOnTouchOutside(true);
//用setView()時候,mView一定要放在show之前
alertDialog.setView(mView);
alertDialog.show();
LayoutParams layoutParams = mView.getLayoutParams();
layoutParams.height = ScreenUtils.getScreenHeight(mContext) - 370;//減去標題欄和按鈕的高度,不同主題的高度不一致
//layoutParams.width = ScreenUtils.getScreenWidth(mContext) - 100;
mView.setLayoutParams(layoutParams);
//用setContextView()則要放在show之後
alertDialog.getWindow().setLayout(ScreenUtils.getScreenWidth(mContext), ScreenUtils.getScreenHeight(mContext) - 90);
}
public void setTitle(String strTitle) {
mTitle = strTitle;
}
public void setProgress(int progress) {
prgTask.setProgress(progress);
}
public void setTaskPercentInfo(String strTaskPercnetInfo) {
txtTaskPercentInfo.setText(strTaskPercnetInfo);
}
public void setDoTask(String strDoWork) {
txtDoTaskName.setText(strDoWork);
}
public void addMsg(String msg) {
mAdapter.addItem(msg);
}
}
Activity中的调用方法:
private ShowTaskProgressDialog dlg;
private Runnable addmsg;
private Handler mHandlerDlg;
private int i = 0;
private void initAlertDialog() {
dlg = new ShowTaskProgressDialog(NFExpoActivity.this);
dlg.setTitle("批量上傳任務");
dlg.show();
dlg.addMsg("開始上傳");
mHandlerDlg = new Handler();
addmsg = new Runnable() {
public void run() {
i++;
String msg = "上傳case" + i;
dlg.setDoTask(msg);
dlg.addMsg(msg);
dlg.setProgress(i);
if (i < 100) {
mHandlerDlg.postDelayed(addmsg, 100);
} else {
i = 0;
dlg.setProgress(i);
dlg.addMsg("結束上傳");
}
}
};
mHandlerDlg.postDelayed(addmsg, 1000);
}
布局文件:
简单的两个TextView和一个ListView和一个ProgressBar进度条。