android 对话框中的进度条 (ProgressDialog)

时间:2023-03-09 12:54:36
android 对话框中的进度条 (ProgressDialog)

from:http://byandby.iteye.com/blog/817214

显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里同样使用了线程来控制进度条显示,另外可以使用以下方法来设置ProgressDialog。 
  setProgressStyle:设置进度条风格,风格为圆形,旋转的。 
  setTitlt:设置ProgressDialog 标题 
  setMessage:设置ProgressDialog提示信息; 
  setIcon:设置ProgressDialog标题图标; 
  setIndeterminate:设置ProgressDialog 的进度条是否不明确; 
  setCancelable:设置ProgressDialog 是否可以按返回键取消; 
  setButton:设置ProgressDialog 的一个Button(需要监听Button事件); 
  show:显示ProgressDialog。

首先还是让我们看看运行效果吧

android 对话框中的进度条 (ProgressDialog) 
android 对话框中的进度条 (ProgressDialog) 
android 对话框中的进度条 (ProgressDialog)

下面先来看看布局文件 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<Button 
android:id="@+id/Button01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="圆形进度条"/>

<Button 
android:id="@+id/Button02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="长形进度条"/> 
</LinearLayout>

Activity01

    1. package xiaohang.zhimeng;
    2. import android.app.Activity;
    3. import android.app.ProgressDialog;
    4. import android.content.DialogInterface;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.view.View.OnClickListener;
    8. import android.widget.Button;
    9. public class Activity01 extends Activity {
    10. private Button xhButton01, xhButton02;
    11. int xh_count = 0;
    12. // 声明进度条对话框
    13. ProgressDialog xh_pDialog;
    14. @Override
    15. public void onCreate(Bundle savedInstanceState) {
    16. super.onCreate(savedInstanceState);
    17. setContentView(R.layout.main);
    18. // 得到按钮对象
    19. xhButton01 = (Button) findViewById(R.id.Button01);
    20. xhButton02 = (Button) findViewById(R.id.Button02);
    21. // 设置xhButton01的事件监听
    22. xhButton01.setOnClickListener(new OnClickListener() {
    23. @Override
    24. public void onClick(View v) {
    25. // 创建ProgressDialog对象
    26. xh_pDialog = new ProgressDialog(Activity01.this);
    27. // 设置进度条风格,风格为圆形,旋转的
    28. xh_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    29. // 设置ProgressDialog 标题
    30. xh_pDialog.setTitle("提示");
    31. // 设置ProgressDialog提示信息
    32. xh_pDialog.setMessage("这是一个圆形进度条对话框");
    33. // 设置ProgressDialog标题图标
    34. xh_pDialog.setIcon(R.drawable.img1);
    35. // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
    36. xh_pDialog.setIndeterminate(false);
    37. // 设置ProgressDialog 是否可以按退回键取消
    38. xh_pDialog.setCancelable(true);
    39. // 设置ProgressDialog 的一个Button
    40. xh_pDialog.setButton("确定", new Bt1DialogListener());
    41. // 让ProgressDialog显示
    42. xh_pDialog.show();
    43. }
    44. });
    45. // 设置xhButton02的事件监听
    46. xhButton02.setOnClickListener(new Button.OnClickListener() {
    47. @Override
    48. public void onClick(View v) {
    49. xh_count = 0;
    50. // 创建ProgressDialog对象
    51. xh_pDialog = new ProgressDialog(Activity01.this);
    52. // 设置进度条风格,风格为圆形,旋转的
    53. xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    54. // 设置ProgressDialog 标题
    55. xh_pDialog.setTitle("提示");
    56. // 设置ProgressDialog提示信息
    57. xh_pDialog.setMessage("这是一个长形进度条对话框");
    58. // 设置ProgressDialog标题图标
    59. xh_pDialog.setIcon(R.drawable.img2);
    60. // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
    61. xh_pDialog.setIndeterminate(false);
    62. // 设置ProgressDialog 进度条进度
    63. xh_pDialog.setProgress(100);
    64. // 设置ProgressDialog 是否可以按退回键取消
    65. xh_pDialog.setCancelable(true);
    66. // 让ProgressDialog显示
    67. xh_pDialog.show();
    68. new Thread() {
    69. @Override
    70. public void run() {
    71. try {
    72. while (xh_count <= 100) {
    73. // 由线程来控制进度
    74. xh_pDialog.setProgress(xh_count++);
    75. Thread.sleep(100);
    76. }
    77. xh_pDialog.cancel();
    78. } catch (Exception e) {
    79. xh_pDialog.cancel();
    80. }
    81. }
    82. }.start();
    83. }
    84. });
    85. }
    86. // xhButton01的监听器类
    87. class Bt1DialogListener implements DialogInterface.OnClickListener {
    88. @Override
    89. public void onClick(DialogInterface dialog, int which) {
    90. // 点击“确定”按钮取消对话框
    91. dialog.cancel();
    92. }
    93. }
    94. }