java类代码:
1 //普通进度对话框 2 public void bt8_onClick(View v) 3 { 4 final ProgressDialog progressDialog = new ProgressDialog(this); 5 progressDialog.setMessage("请等待……"); 6 progressDialog.setTitle("进度对话框"); 7 progressDialog.setCancelable(false); 8 progressDialog.show(); 9 //不要写业务代码 10 //开启子线程 11 //实现多线程:1.继承 2.实现接口 12 //1.继承Thread,重写run方法,再调用start()方法 13 new Thread(){ 14 @Override 15 public void run() { //{}内表示继承 16 //业务代码 17 try { 18 //延时 线程 19 Thread.sleep(3000); 20 } 21 catch (Exception e) 22 { 23 24 } 25 //执行完业务代码 26 //关闭 27 progressDialog.dismiss(); 28 } 29 }.start();//{}start负责启动子线程,自动执行run()方法 30 } 31 //水平进度对话框 32 public void bt9_onClick(View v) 33 { 34 final ProgressDialog progressDialog = new ProgressDialog(this); 35 progressDialog.setTitle("水平进度对话框"); 36 progressDialog.setMessage("正在加载……"); 37 progressDialog.setCancelable(false); 38 //设置成水平 39 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 40 progressDialog.show(); 41 42 final TextView tv_2 = (TextView)findViewById(R.id.tv_2); 43 44 //启动子线程 实习接口 Runnable,run() 45 new Thread(new Runnable() { 46 @Override 47 public void run() { 48 //模拟进度变化 49 for (int i = 0; i <=100; i++){ 50 progressDialog.setProgress(i); 51 try { 52 //延时 线程 53 Thread.sleep(200); 54 } 55 catch (Exception e) 56 { 57 58 } 59 } 60 //在子线程里访问UI线程的View 61 runOnUiThread(new Runnable() { 62 @Override 63 public void run() { 64 65 tv_2.setText("下载完成"); 66 } 67 }); 68 progressDialog.dismiss(); 69 } 70 }).start(); 71 }
注意开启多线程的方法
附思维导图: