ProgressDialog的使用:请稍等提示

jopen 9年前

 

我们经常看到有些应用中会出现 “ 请稍等 ”  对话框,Android中ProgressDialog类可以实现该效果

O(∩_∩)O~

layout/activity_main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>    <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="horizontal"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:padding="20dp"    >      <Button        android:id="@+id/circleButt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:text="圆形进度条"        />    <Button      android:id="@+id/longButt"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_marginLeft="60dp"      android:text="长形进度条"      />    </LinearLayout>

创建一个ProgressDialogExample类继承Activity
package com.example.lenovo.a1101;    import android.app.Activity;  import android.app.ProgressDialog;  import android.content.DialogInterface;  import android.os.Bundle;  import android.os.PersistableBundle;  import android.preference.DialogPreference;  import android.view.View;  import android.widget.Button;  import android.widget.Toast;    /**   * Created by Lenovo on 2015/11/1.   */  public class ProgressDialogExample extends Activity {      Button circleBut;      Button longBut;      ProgressDialog myDialog;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          circleBut = (Button) findViewById(R.id.circleButt);          longBut = (Button) findViewById(R.id.longButt);          circleBut.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  myDialog = new ProgressDialog(ProgressDialogExample.this);                                        //创建progressDialog对象                  myDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);                                          //设置进度条为圆形样式                  myDialog.setTitle("提示");                                         //设置进度条的标题信息                  myDialog.setMessage("主人,小白正在努力加载中……");                                         //设置进度条的提示信息                  myDialog.setIcon(R.drawable.ic_launch);                                         //是指进度条的图标                  myDialog.setIndeterminate(false);                                      //设置进度条是否为不明确                  myDialog.setCancelable(true);                  //为进度条添加"确定"按钮,并添加点击事件                  myDialog.setButton("确定", new DialogInterface.OnClickListener() {                      @Override                      public void onClick(DialogInterface dialog, int which) {                          myDialog.cancel();                      }                  });                  myDialog.show();              }          });          longBut.setOnClickListener(new View.OnClickListener() {              int count = 0;//存储进度条当前的进度值,初始值为0                @Override              public void onClick(View v) {                    myDialog = new ProgressDialog(ProgressDialogExample.this);                  myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);                  myDialog.setTitle("提示");                  myDialog.setMessage("快马加鞭加载中……");                  myDialog.setIcon(R.drawable.ic_launch);                  myDialog.setIndeterminate(false);                  myDialog.setCancelable(true);                  myDialog.setMax(100);                                   //设置进度条的最大值                  myDialog.setProgress(0);                                      //默认值为0                  myDialog.setSecondaryProgress(50);                                         //设置第二进度条的长度                  myDialog.setButton("取消", new DialogInterface.OnClickListener() {                        @Override                      public void onClick(DialogInterface dialog, int which) {                          myDialog.cancel();                      }                    });                  myDialog.show();//显示进度条                   new Thread() {             //定义线程,动态改变当前进度条的值                      public void run() {                          while (count <= 100) {                              myDialog.setProgress(count++);                              try {                                  Thread.sleep(100);                              } catch (InterruptedException e) {                                  e.printStackTrace();                              }                          }                        }                    }.start();                }          });      }  }