1、布局文件progress_dialog_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_progress_dialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_progress_dialog_text" android:layout_gravity="center_horizontal"/> </LinearLayout>
2、Activity类ProgressDialogActivity.java
package cn.luxh.dialognew; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ProgressDialogActivity extends Activity { private Button btn_progress_dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progress_dialog_activity); handleBtnProgressDialogClick(); } /** * 处理按钮事件 */ private void handleBtnProgressDialogClick() { btn_progress_dialog = (Button) findViewById(R.id.btn_progress_dialog); btn_progress_dialog.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { createProgressDialog(); } }); } /** * 生成进度对话框 */ private void createProgressDialog() { //显示进度对话框 final ProgressDialog progressDialog = ProgressDialog.show(this, "提示", "处理中..."); //业务处理 new Thread(new Runnable(){ @Override public void run() { try { Thread.sleep(10000); progressDialog.dismiss();//取消进度对话框 } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } }
3、运行效果