mian.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/information" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="progressdialog"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="圆形"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水平条"/> </LinearLayout>
java:
package Android2.test; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Android2Activity extends Activity { private Button Button1,Button2; int m_count = 0; //声明进度条对话框 ProgressDialog progressdialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //得到按钮对象 Button1 = (Button)findViewById(R.id.button1); Button2 = (Button)findViewById(R.id.button2); //设置Button1的事件监听 Button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub //创建ProgressDialog对象 progressdialog = new ProgressDialog(Android2Activity.this); // 设置进度条风格,风格为圆形,旋转的 progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置ProgressDialog 标题 progressdialog.setTitle("提示"); // 设置ProgressDialog 提示信息 progressdialog.setMessage("正在缓冲中,请稍等....."); // 设置ProgressDialog 标题图标 // m_pDialog.setIcon(R.drawable.img1); // 设置ProgressDialog 的进度条是否不明确 progressdialog.setIndeterminate(false); // 设置ProgressDialog 是否可以按退回按键取消 progressdialog.setCancelable(true); // 设置ProgressDialog 的一个Button progressdialog.setButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { //点击“确定按钮”取消对话框 dialog.cancel(); } }); // 让ProgressDialog显示 progressdialog.show(); } }); //设置Button2的事件监听 Button2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub m_count = 0; // 创建ProgressDialog对象 progressdialog = new ProgressDialog(Android2Activity.this); // 设置进度条风格,风格为长形 progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置ProgressDialog 标题 progressdialog.setTitle("提示"); // 设置ProgressDialog 提示信息 progressdialog.setMessage("正在缓冲中,请稍等....."); // 设置ProgressDialog 标题图标 // m_pDialog.setIcon(R.drawable.img2); // 设置ProgressDialog 进度条进度 progressdialog.setProgress(100); // 设置ProgressDialog 的进度条是否不明确 progressdialog.setIndeterminate(false); // 设置ProgressDialog 是否可以按退回按键取消 progressdialog.setCancelable(true); // 让ProgressDialog显示 progressdialog.show(); new Thread() { public void run() { try { while (m_count <= 100) { // 由线程来控制进度。 progressdialog.setProgress(m_count++); Thread.sleep(100); } progressdialog.cancel(); } catch (InterruptedException e) { progressdialog.cancel(); } } }.start(); } }); } }