陈斌彬的技术博客

Stay foolish,stay hungry

Android显示“正在加载,请稍后...”

如何实现点击按钮,弹出“正在加载数据,请稍候…”对话框,加载完了之后,对话框自动消失呢?

其实也是很简单的看下面代码:

import com.farsunset.ichat.example.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;

public class DateReportActivity extends Activity {

  private ProgressDialog progressDialog;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date_report);

    showProgressDialog("提示", "正在加载......");
  }

  /*
   * 提示加载
   */
  public void showProgressDialog(String title, String message) {
    if (progressDialog == null) {

      progressDialog = ProgressDialog.show(DateReportActivity.this, title,
          message, true, false);
    } else if (progressDialog.isShowing()) {
      progressDialog.setTitle(title);
      progressDialog.setMessage(message);
    }

    progressDialog.show();

  }

  /*
   * 隐藏提示加载
   */
  public void hideProgressDialog() {

    if (progressDialog != null && progressDialog.isShowing()) {
      progressDialog.dismiss();
    }

  }

}

至于XML文件写什么就不用管了,只要你在做项目的时候用到联网程序,就可以用以上的两个方法了

Resource Reference