一.进度对话框 ProgressDialog
1.用法
1-new progressDialog(Context)
2-setTitle (标题)
3-setMessage (信息)
4-show() 显示
5-setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 设置样式为水平进度
6-支持跨线程访问
2.多线程
1-负责执行耗时较长的业务代码,执行完之后在关闭进度对话框
2-用法
1°继承
1>继承Thread,重写run(),调用start()启动子线程
2>new Thread(){public void run(){业务代码; 关闭对话框;}}.start();
2°实现接口
1>实现Runnable接口,传给Thread(),调用start()
2>new Thread(new Runnable(){public void run(){业务代码;关闭对话框;}}),start();
3-跨线程访问主线程的组件
runOnUiThread(new Runnable(){public void run(){访问主线程组件的代码;}})
进度对话框实现代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.wang.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 13 <Button 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="普通进度对话框" 17 android:onClick="bt8_OnClick" 18 /> 19 20 <Button 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:text="水平进度对话框" 24 android:onClick="bt9_OnClick" 25 /> 26 <TextView 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:text="运行结果" 30 android:id="@+id/tv_2"/> 31 32 33 </LinearLayout>
1 package com.example.wang.testapp2; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.ProgressDialog; 6 import android.app.TimePickerDialog; 7 import android.content.DialogInterface; 8 import android.opengl.ETC1Util; 9 import android.support.v7.app.AppCompatActivity; 10 import android.os.Bundle; 11 import android.view.LayoutInflater; 12 import android.view.View; 13 import android.widget.DatePicker; 14 import android.widget.EditText; 15 import android.widget.ImageView; 16 import android.widget.TextView; 17 import android.widget.TimePicker; 18 import android.widget.Toast; 19 20 import java.util.Calendar; 21 22 public class TestActivity5 extends AppCompatActivity { 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_test5); 28 29 } 30 31 32 //普通进度对话框 33 public void bt8_OnClick(View v) { 34 final ProgressDialog progressDialog = new ProgressDialog(this); 35 36 progressDialog.setMessage("请等待..."); 37 progressDialog.setTitle("进度对话框"); 38 progressDialog.setCancelable(false); 39 40 progressDialog.show(); 41 42 43 //开启子线程 44 //实现多线程:1-继承 2-实现接口 45 46 //1.继承Thread,重写run(),调用start() 47 new Thread() { 48 @Override 49 public void run() { 50 51 //业务代码 52 53 try { 54 //延时 线程 55 Thread.sleep(3000); 56 57 } catch (Exception e) { 58 59 } 60 61 //执行完业务代码 62 //关闭 63 progressDialog.dismiss(); 64 } 65 }.start();//start负责启动子线程,自动执行run() 66 67 } 68 69 70 //水平进度对话框 71 public void bt9_OnClick(View v) 72 { 73 final ProgressDialog progressDialog=new ProgressDialog(this); 74 75 progressDialog.setTitle("水平进度对话框"); 76 progressDialog.setMessage("正在加载..."); 77 progressDialog.setCancelable(false); 78 79 //设置成水平 80 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 81 82 progressDialog.show(); 83 84 final TextView tv_2=(TextView)findViewById(R.id.tv_2); 85 86 //启动子线程 实现接口 Runnable,run() 87 new Thread(new Runnable() { 88 @Override 89 public void run() { 90 91 //模拟进度变化 92 93 for (int i=0;i<=100;i++) { 94 95 progressDialog.setProgress(i); 96 97 try { 98 //延时 线程 99 Thread.sleep(200); 100 101 } catch (Exception e) { 102 103 } 104 } 105 106 //在子线程里访问UI线程的View 107 runOnUiThread(new Runnable() { 108 @Override 109 public void run() { 110 111 tv_2.setText("下载完成"); 112 113 } 114 }); 115 116 //执行完业务代码 117 //关闭 118 progressDialog.dismiss(); 119 } 120 }).start(); 121 122 } 123 }
1.普通进度对话框
2.水平进度对话框
小结: