布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:orientation="vertical"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".DialogActivity" >        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/text" />        <Button           android:id="@+id/btn_dialog"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:onClick="onClick"          android:text="多选对话框"          />      <Button           android:id="@+id/btn_dialog1"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:onClick="onClick1"          android:text="进度对话框"          />      <Button           android:id="@+id/btn_dialog2"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:onClick="onClick2"          android:text="详细进度对话框"          />  </LinearLayout>  

DialogActivity.java

package com.dialog;    import android.app.Activity;  import android.app.AlertDialog;  import android.app.AlertDialog.Builder;  import android.app.Dialog;  import android.app.ProgressDialog;  import android.content.DialogInterface;  import android.os.Bundle;  import android.view.View;  import android.widget.Toast;    public class DialogActivity extends Activity {            //CharSequence接口      CharSequence[] items = {"谷歌","苹果","百度","微软"};      boolean[] itemsChecked = new boolean[items.length];            //创建ProgressDialog类的一个实例      ProgressDialog progressDialog = null;             protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);      }              //重写onCreateDialog()方法      protected Dialog onCreateDialog(int id, Bundle args) {          switch (id) {          case 0:              /*              * AlertDialog Dialog的子类 显示进度指示器和可选的文本消息或视图的对话框              * 多选对话框              */              Builder builder = new AlertDialog.Builder(this);              builder.setIcon(R.drawable.ic_launcher);              builder.setTitle("你喜欢哪家互联网公司");                            //响应确定按钮              builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {                                    //Toast并没有在DialogActivity直接使用,而是在AlertDialog类中使用  因此需要用getBaseContext()返回一个Context类的实例                  public void onClick(DialogInterface dialog, int whichButton) {                      Toast.makeText(getBaseContext(), "确定按钮被点击", Toast.LENGTH_SHORT).show();                  }              });                            //响应取消按钮              builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){                    public void onClick(DialogInterface dialog, int whichButton) {                      Toast.makeText(getBaseContext(), "取消按钮被点击", Toast.LENGTH_SHORT).show();                  }              });                            //响应多选事件              builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {                                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {                      Toast.makeText(getBaseContext(), (isChecked?"选中":"未选中")+items[which], Toast.LENGTH_SHORT).show();                  }              });              return builder.create();                        case 1:              /*              * 设置图标 标题 样式 按钮              * 显示详细进度对话框              */              progressDialog = new ProgressDialog(this);              progressDialog.setIcon(R.drawable.ic_launcher);              progressDialog.setTitle("下载文件...");              progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);              progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener() {                                    public void onClick(DialogInterface dialog, int which) {                      Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show();                  }              });              progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",new DialogInterface.OnClickListener() {                                    public void onClick(DialogInterface dialog, int which) {                      Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();                  }              });              return progressDialog;          }                    return null;      }            public void onClick(View v){          //调用onCreateDialog()方法          showDialog(0);      }            /*      * 进度对话框      */      public void onClick1(View v){          final ProgressDialog dialog = ProgressDialog.show(this, "请等待3秒", "Please wait...",true);          //启动一个新线程          new Thread(new Runnable() {              public void run() {                  try {                      Thread.sleep(3000);                      //解除对话框 从屏幕上移除 此方法是线程安全的                      dialog.dismiss();                  } catch (InterruptedException e) {                      e.printStackTrace();                  }              }          }).start();      }            /*      * 详细进度对话框      */      public void onClick2(View v){          showDialog(1);          //设置初始值为0          progressDialog.setProgress(0);                    new Thread(new Runnable() {                  public void run(){                      for (int i = 1; i <= 15; i++) {                          try {                              Thread.sleep(1000);                              //更新对话框                              progressDialog.incrementProgressBy((int)100/15);                          } catch (InterruptedException e) {                              e.printStackTrace();                          }                      }                      progressDialog.dismiss();              }          }).start();                }    }  

android进度对话框的使用android进度对话框的使用android进度对话框的使用

随笔系原创 转载请注明

欢迎访问我的博客

更多相关文章

  1. Android Studio点击按钮更换背景图片
  2. Android 进度条
  3. android进度条的样式
  4. 2.5.3 使用alertDialog创建自定义对话框
  5. Android 自定义圆角按钮
  6. 常用的android弹出对话框alertDialog
  7. 2010.12.22——— android 一个永远处于底部的按钮集合
  8. 对话框式Activity的设置

随机推荐

  1. Android Animation动画(Frame-By-Frame An
  2. android > android 客户端 ,PHP 服务器端
  3. 2011.12.05(4)——— android JNI学习之三
  4. 解析错误:解析软件包时出现问题
  5. Android 动画框架代码分析
  6. This android SDk requires Android deve
  7. 浅谈Android下拉菜单Spinner
  8. Error:(15) No resource identifier foun
  9. android执行外部程序,类似DELPHI里的EXEC
  10. android 读取,写入图片到sd卡源码