1. 先定义一个Dialog类,以供调用
public static VProgressDialog createDialog(Activity activity) { dialog = new VProgressDialog(activity, R.style.Theme_DialogActivity); dialog.setContentView(R.layout.loading); dialog.getWindow().getAttributes().gravity = Gravity.CENTER; dialog.setCancelable(true); dialog.setCanceledOnTouchOutside(false); return dialog; } // 对话框中显示的信息 public VProgressDialog setMessage(String msg) { if (dialog != null && !TextUtils.isEmpty(msg)) { TextView tvMsg = (TextView) dialog.findViewById(R.id.tvTextToast); tvMsg.setText(msg); } return dialog; }
2. 对话话显示和隐藏方法
private void showProgress(String msg) { dismissProgress(); if (progressDialog == null) progressDialog = VProgressDialog.createDialog(MainActivity.this); progressDialog.setMessage(msg); progressDialog.show(); } private void dismissProgress() { if (progressDialog != null && progressDialog.isShowing()) progressDialog.dismiss(); }
3. 调用方法并设置值
showProgress("正在查询...");
dismissProgress();