布局文件:

<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();                }    }  

随笔系原创 转载请注明

欢迎访问我的博客

更多相关文章

  1. Android(安卓)Studio点击按钮更换背景图片
  2. Android(安卓)进度条
  3. 布局技巧和列表控件
  4. android进度条的样式
  5. android代码实现背景切换
  6. Android(安卓)Studio创建计算器Demo
  7. 2.5.3 使用alertDialog创建自定义对话框
  8. android 设置Button或者ImageButton的背景透明
  9. Android(安卓)API 中文(13) —— ToggleButton

随机推荐

  1. 动画:什么是 BF 算法 ?
  2. 高考前一天,六月六号,加一!
  3. 前 K 个高频元素告诉你桶排序有啥用
  4. 看《长安十二时辰》可以了解哪些算法知识
  5. 你知道什么是漂亮排序法吗?哦,知道,不就是臭
  6. Hard 级别难度?桶排序几行代码搞定!
  7. 面试官,你再问我滑动窗口问题试试?我有解题
  8. 扫雷与算法:如何随机化的布雷(一)
  9. 趣味算法图解,高清无码图免费下载
  10. 从简单二叉树问题重新来看深度优先搜索