一.进度对话框 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(){访问主线程组件的代码;}})
进度对话框
package com.hanqi.testapp2;import android.app.AlertDialog;import android.app.DatePickerDialog;import android.app.ProgressDialog;import android.app.TimePickerDialog;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.DatePicker;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import android.widget.TimePicker;import android.widget.Toast;import java.util.Calendar;public class TestActivity5 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test5); } //普通进度对话框 public void bt8_OnClick(View v) { final ProgressDialog progressDialog=new ProgressDialog(this); progressDialog.setMessage("请等待..."); progressDialog.setTitle("进度对话框"); progressDialog.setCancelable(false); progressDialog.show(); //开启子线程 //实现多线程:1.继承 2.实现接口 //1.继承Thread,重写run()方法,调用start() new Thread(){ @Override public void run() { //业务代码 try { //延时 Thread.sleep(3000); } catch (Exception e) { } //执行完业务代码之后 //关闭 progressDialog.dismiss(); } }.start();//start负责启动多线程,自动执行run() } //水平进度对话框 public void bt9_OnClick(View v) { final ProgressDialog progressDialog=new ProgressDialog(this); progressDialog.setTitle("水平进度对话框"); progressDialog.setMessage("正在加载..."); progressDialog.setCancelable(false); //设置成水平 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); final TextView tv_2=(TextView)findViewById(R.id.tv_2); //启动子线程 实现接口 Runnable,run() new Thread(new Runnable() { @Override public void run() { //模拟进度变化 for(int i=0;i<=100;i++) { progressDialog.setProgress(i); try { //延时 线程 Thread.sleep(200); } catch (Exception e) { } } //在子线程里访问UI线程的View runOnUiThread(new Runnable() { @Override public void run() { tv_2.setText("下载完成"); } }); //执行完业务代码之后 //关闭 progressDialog.dismiss(); } }).start(); }}进度对话框