杭州seo相关网站seo产品优化免费软件
我们创建进度条对话框的方式有两种:
- 1.直接调用ProgressDialog提供的静态方法show()显示
- 2.创建ProgressDialog,再设置对话框的参数,最后show()出来
效果图如下:
MainActivity.java的代码如下:
package com.deepreality.progressdialogdemo;import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Button btnNormalDialog, btnLinearDialog, btnLiearDialogWithUpdateProgress;private int currentProgress = 0;private int add = 0;private ProgressDialog pd1 = null;private ProgressDialog pd2 = null;private final static int MAXVALUE = 100;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);componentInit();componentAddOnClickListener();}Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 123) {pd2.setProgress(currentProgress);}if (currentProgress >= MAXVALUE) {pd2.dismiss();}}};public void componentInit() {btnNormalDialog = findViewById(R.id.main_btnNormalDialog);btnLinearDialog = findViewById(R.id.main_btnLinearDialog);btnLiearDialogWithUpdateProgress = findViewById(R.id.main_btnLinearDialogWithUpdateProgress);}public void componentAddOnClickListener() {btnNormalDialog.setOnClickListener(this);btnLinearDialog.setOnClickListener(this);btnLiearDialogWithUpdateProgress.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.main_btnNormalDialog:{//这里的话参数依次为,上下文,标题,内容,是否显示进度,是否可以用取消按钮关闭ProgressDialog.show(MainActivity.this, "资源加载中", "资源加载中,请稍后...",false,true);break;}case R.id.main_btnLinearDialog:{pd1 = new ProgressDialog(MainActivity.this);//依次设置标题,内容,是否用取消按钮关闭,是否显示进度pd1.setTitle("软件更新中");pd1.setMessage("软件正在更新中,请稍后...");pd1.setCancelable(true);//这里是设置进度条的风格,HORIZONTAL是水平进度条,SPINNER是圆形进度条pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd1.setIndeterminate(true);//调用show()方法将ProgressDialog显示出来pd1.show();break;}case R.id.main_btnLinearDialogWithUpdateProgress:{//初始化属性currentProgress = 0;add = 0;//依次设置一些属性pd2 = new ProgressDialog(MainActivity.this);pd2.setMax(MAXVALUE);pd2.setTitle("文件读取中");pd2.setMessage("文件加载中,请稍后...");//这里设置为不可以通过按取消按钮关闭进度条pd2.setCancelable(false);pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//这里设置的是是否显示进度,设为false才是显示的哦!pd2.setIndeterminate(false);pd2.show();//这里的话新建一个线程,重写run()方法,new Thread(){public void run(){while(currentProgress < MAXVALUE){//这里的算法是决定进度条变化的,可以按需要写currentProgress = 2 * usetime() ;//把信息码发送给handle让更新界面handler.sendEmptyMessage(123);}}}.start();break;}default:break;}}//这里设置一个耗时的方法:private int usetime() {add++;try{Thread.sleep(100);}catch (InterruptedException e) {e.printStackTrace();}return add;}
}