1 public class MainActivity extends Activity implements OnClickListener { 2 private ProgressBar progressBar, progressBar2; 3 private TextView textView; 4 private Button add, reduce, reset; 5 private ProgressDialog progressDialog; 6 private Button button; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.fragment_main); 12 progressBar = (ProgressBar) findViewById(R.id.progressBar1); 13 progressBar2 = (ProgressBar) findViewById(R.id.progressBar2); 14 textView = (TextView) findViewById(R.id.textView1); 15 add = (Button) findViewById(R.id.add); 16 reduce = (Button) findViewById(R.id.reduce); 17 reset = (Button) findViewById(R.id.reset); 18 19 button = (Button) findViewById(R.id.button1); 20 button.setOnClickListener(this); 21 22 // 获取第一条进度条的进度 23 int first = progressBar2.getProgress(); 24 25 // 获取第二条进度条的进度 26 int second = progressBar2.getSecondaryProgress(); 27 // 获取进度条最大进度 28 int max = progressBar2.getMax(); 29 textView.setText("第一进度百分百:" + (int) (first / (float) max * 100) + "%" 30 + (int) (second / (float) max * 100) + "%"); 31 add.setOnClickListener(this); 32 reduce.setOnClickListener(this); 33 reset.setOnClickListener(this); 34 } 35 36 @Override 37 public void onClick(View v) { 38 // TODO Auto-generated method stub 39 switch (v.getId()) { 40 case R.id.add: { 41 // 增加第一进度第二进度10刻度 42 progressBar2.incrementProgressBy(10); 43 progressBar2.incrementSecondaryProgressBy(10); 44 break; 45 } 46 47 case R.id.reduce: { 48 // 减少第一进度第二进度10刻度 49 progressBar2.incrementProgressBy(-10); 50 progressBar2.incrementSecondaryProgressBy(-10); 51 break; 52 } 53 54 case R.id.reset: { 55 progressBar2.setProgress(50); 56 progressBar2.setSecondaryProgress(80); 57 break; 58 } 59 case R.id.button1: { 60 // 页面风格: 61 // 1.新建progressDialog对象 62 progressDialog = new ProgressDialog(MainActivity.this); 63 progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); 64 // 设置标题 65 progressDialog.setTitle("下载;"); 66 // 设置对话框里的文字信息 67 progressDialog.setMessage("欢迎大家下载"); 68 // 设置图标 69 progressDialog.setIcon(R.drawable.ic_launcher); 70 71 // progreBar的一些属性 72 progressDialog.setMax(100); 73 // 设置初始化的进度 74 progressDialog.incrementProgressBy(50); 75 // 进度条是明确显示进度的 76 progressDialog.setIndeterminate(false); 77 // 设置一个确定按钮 78 progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", 79 new DialogInterface.OnClickListener() { 80 81 @Override 82 public void onClick(DialogInterface dialog, int which) { 83 // TODO Auto-generated method stub 84 Toast.makeText(MainActivity.this, "hahahha,你来了", 1) 85 .show(); 86 } 87 }); 88 // 是否可以通过返回按钮退出对话框 89 progressDialog.setCancelable(true); 90 // 显示progressDialog 91 progressDialog.show(); 92 break; 93 } 94 } 95 textView.setText("第一进度百分百:" 96 + (int) (progressBar2.getProgress() 97 / (float) progressBar2.getMax() * 100) 98 + "%" 99 + (int) (progressBar2.getSecondaryProgress() 100 / (float) progressBar2.getMax() * 100) + "%"); 101 } 102 103 }
自定义进度条:
在main.xml文件下的<Progress>的属性style改成:style="@android:style/Widget.ProgressBar.Horizontal" 然后,按住Ctrl,左击"@android:style/Widget.ProgressBar.Horizontal",就进入 styles.xml,这时将看到<style name="Widget.ProgressBar.Horizontal">,在其属性中有<item name="progressDrawable">@drawable/progress_horizontal</item> ,再次按住Ctrl并左击该属性,就进入了进度条样式的文件。 创建完样式后,在 main.xml中的<Progress>里面添加属性: android:progressDrawable="@drawable/progress_bar" 就会覆盖原有的样式,实现了自定义进度条样式的效果。
Android攻城狮 progressBar&progressDialog
原文:http://www.cnblogs.com/my334420/p/6709128.html