资源包:范例练习b8 FLExB8.rar
需求分析:两种类型的ProgressDialog,STYLE_SPINNER和STYLE_HORIZONTAL
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.example.flexb8; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.app.Activity; 7 import android.app.ProgressDialog; 8 import android.content.DialogInterface; 9 import android.view.Menu; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.TextView; 14 15 public class MainActivity extends Activity { 16 17 private TextView tv1; 18 private Button bt1; 19 private Button bt2; 20 private int progressCount; 21 private ProgressDialog progressDialog; 22 private ProgressDialog progressDialog2; 23 24 @Override 25 public void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 bt1 = (Button)findViewById(R.id.bt1); 30 bt1.setOnClickListener(onClickListenter); 31 bt2 = (Button)findViewById(R.id.bt2); 32 bt2.setOnClickListener(onClickListenter); 33 tv1 = (TextView)findViewById(R.id.tv1); 34 } 35 36 OnClickListener onClickListenter = new OnClickListener() { 37 //@Override 38 @SuppressWarnings("deprecation") 39 public void onClick(View v) { 40 switch(v.getId()){ 41 case R.id.bt1: 42 progressDialog = new ProgressDialog(MainActivity.this); 43 //风格 44 progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 45 progressDialog.setTitle("Title"); 46 progressDialog.setMessage("Message"); 47 progressDialog.setIcon(R.drawable.ic_launcher); 48 //进度不明确false 即明确 49 progressDialog.setIndeterminate(false); 50 //按回退键取消 51 progressDialog.setCancelable(true); 52 //写了这个Button就木有代码提示了,这...何解啊 53 /*progressDialog.setButton("确定", new DialogInterface.OnClickListener() { 54 //@Override 55 public void onClick(DialogInterface dialog, int which) { 56 progressDialog.cancel(); 57 } 58 });*/ 59 progressDialog.show(); 60 break; 61 62 case R.id.bt2: 63 progressCount = 0; 64 progressDialog2 = new ProgressDialog(MainActivity.this); 65 progressDialog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 66 progressDialog2.setTitle("Title2"); 67 progressDialog2.setIcon(R.drawable.ic_launcher); 68 progressDialog2.setMessage("Message2"); 69 progressDialog2.setIndeterminate(false); 70 progressDialog2.setCancelable(true); 71 progressDialog2.setMax(100); 72 new Thread(){ 73 @Override 74 public void run(){ 75 while(progressCount<=100){ 76 try { 77 progressDialog2.setProgress(progressCount++); 78 Thread.sleep(200); 79 } catch (InterruptedException e) { 80 progressCount = 0; 81 progressDialog2.cancel(); 82 } 83 } 84 } 85 }.start(); 86 progressDialog2.setButton("确定", new DialogInterface.OnClickListener() { 87 //@Override 88 public void onClick(DialogInterface dialog, int which) { 89 dialog.cancel(); 90 } 91 }); 92 progressDialog2.show(); 93 break; 94 95 default: 96 break; 97 }//switch 98 } 99 }; 100 101 102 @Override 103 public boolean onCreateOptionsMenu(Menu menu) { 104 getMenuInflater().inflate(R.menu.activity_main, menu); 105 return true; 106 } 107 } 108 //http://www.cnblogs.com/jh5240/archive/2011/10/30/2229269.html