显示进度对话框

作者:追风剑情 发布于:2015-7-5 18:54 分类:Android

  1. package com.example.androidtest;
  2.  
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.app.ProgressDialog;
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.Toast;
  15.  
  16. public class ProgressDialogActivity extends Activity implements OnClickListener{
  17.  
  18. String tag = "ProgressDialogActivity";
  19. ProgressDialog progressDialog;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_progress_dialog);
  24. Button btn1 = (Button)this.findViewById(R.id.button1);
  25. btn1.setOnClickListener(this);
  26. Button btn2 = (Button)this.findViewById(R.id.button2);
  27. btn2.setOnClickListener(this);
  28. }
  29. public void onClick(View v)
  30. {
  31. int id = v.getId();
  32. switch(id){
  33. case R.id.button1:
  34. showDialog(0);
  35. break;
  36. case R.id.button2:
  37. showDialog(1);
  38. progressDialog.setProgress(0);
  39. //模拟加载进度
  40. new Thread(new Runnable(){
  41. public void run(){
  42. for(int i=1; i<=15; i++){
  43. try{
  44. Thread.sleep(1000);
  45. progressDialog.incrementProgressBy((int)(100/15));
  46. } catch (InterruptedException e){
  47. }
  48. }
  49. progressDialog.dismiss();
  50. }
  51. }).start();
  52. break;
  53. }
  54. }
  55. @Override
  56. protected Dialog onCreateDialog(int id)
  57. {
  58. switch(id)
  59. {
  60. case 0://显示Please wait...对话框
  61. final ProgressDialog dialog = ProgressDialog.show(this, "title", "Please wait...", true);
  62. new Thread(new Runnable(){
  63. public void run(){
  64. try {
  65. //模拟一个执行5秒种的操作
  66. Thread.sleep(5000);
  67. dialog.dismiss();//关闭对话框
  68. } catch (InterruptedException e){
  69. e.printStackTrace();
  70. }
  71. }
  72. }).start();
  73. break;
  74. case 1://显示有加载进度的对话框
  75. progressDialog = new ProgressDialog(this);
  76. progressDialog.setIcon(R.drawable.ic_launcher);
  77. progressDialog.setTitle("Downloading files...");
  78. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  79. progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
  80. new DialogInterface.OnClickListener() {
  81. @Override
  82. public void onClick(DialogInterface dialog, int which) {
  83. Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();
  84. }
  85. });
  86. progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
  87. new DialogInterface.OnClickListener() {
  88. @Override
  89. public void onClick(DialogInterface dialog, int which) {
  90. Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
  91. }
  92. });
  93. return progressDialog;
  94. }
  95. return null;
  96. }
  97. }

运行效果(没有进度条的)

wait.png

运行效果(有进度条的)

progress.png

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号