Android中八种dialog对话框代码

    public class MainActivity extends Activity {  
      
        private static final int MAX_PROGRESS = 100;  
        private static final int PRO = 10;  
        private Handler handler;  
      
        private int progress=10;  
        private ProgressDialog progressDialog;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            //显示视图  
            setContentView(R.layout.activity_main);  
      
        }  
      
        public void openDialog(View v) {  
              
            //调用 方法  
            //test1();  
            test8();  
            //myDialog();  
      
        }  
      
//第一种 对话框
        public void test1() {  
            // 创建对话框对象  
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();        
            // 设置对话框的标题  
            alertDialog.setTitle("XXXXX");  
            // 设置对话框中的内容  
            alertDialog.setMessage("消息");  
            // 显示对话框  
            alertDialog.show();  
        }  
      
//第二种 对话框
        public void test2() 
{  
            AlertDialog alertDialog = new AlertDialog.Builder(this)  
                    .setTitle("xxxx").setMessage("xxxx").show();  
        }  
      
        //第三种 对话框  
        public void test3() 
{  
            new AlertDialog.Builder(this)  
                    .setIcon(R.drawable.ic_launcher)  
                    .setTitle("xxxx")  
                    .setMessage("是否创建文件")  
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {  
      
                        @Override  
                        public void onClick(DialogInterface dialog, int which) {  
                            // 创建文件了  
                            new AlertDialog.Builder(MainActivity.this).setMessage(  
                                    "文件已经被创建").show();  
                        }  
                    })  
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {  
      
                        @Override  
                        public void onClick(DialogInterface dialog, int which) {  
                            new AlertDialog.Builder(MainActivity.this)  
                                    .setMessage("您已经选择了取消的按钮,该文件不会被创建").create()  
                                    .show();  
      
                        }  
                    }).show();  
        }  

        //第四种 对话框       
        public void test4()
{  
            final String items[] = { "Java", "PHP", "3G", ".NET" };  
      
            new AlertDialog.Builder(this).setTitle("简单列表对话框")  
                    .setItems(items, new DialogInterface.OnClickListener() {  
      
                        @Override  
                        public void onClick(DialogInterface dialog, int which) {  
                            // 第一个参数 dialog int which 那个条目  
                            Toast.makeText(getApplicationContext(),  
                                    "xxxxx" + items[which], Toast.LENGTH_LONG)  
                                    .show();  
      
                        }  
                    }).show();  
        }  

        //第五种 对话框       
        public void test5() 
{  
            final String items[] = { "Java", "PHP", "3G", ".NET" };  
      
            new AlertDialog.Builder(this).setTitle("单选列表对话框")  
      
            // 数据集中的某一列会作为列表对话框的数据加载的列表框中,该参数表示该列的名称(字段名称)  
                    .setSingleChoiceItems(items, 1,  
                            new DialogInterface.OnClickListener() {  
      
                                @Override  
                                public void onClick(DialogInterface dialog,  
                                        int which) {  
                                    // 第一个参数 dialog int which 那个条目  
                                    Toast.makeText(getApplicationContext(),  
                                            "xxxxx" + items[which],  
                                            Toast.LENGTH_LONG).show();  
                                }  
                            }).show();     
        }  

        //第六种 对话框 
        public void test6() 
{      
            final String items[] = { "Java", "PHP", "3G", ".NET" }; 
            new AlertDialog.Builder(this)  
                    .setTitle("多选列表对话框")  
                  
                    .setMultiChoiceItems(items,  
                            new boolean[] { false, true, true, false },  
                            new DialogInterface.OnMultiChoiceClickListener() {  
      
                                @Override  
                                public void onClick(DialogInterface dialog,int which, boolean isChecked) 
{  
                                    if (isChecked) {  
                                        Toast.makeText(getApplicationContext(), "xxx" + items[which],Toast.LENGTH_LONG).show();  
                                    }      
                                }  
                            })  
                    .setPositiveButton("确认", new DialogInterface.OnClickListener() {  
      
                        @Override  
                        public void onClick(DialogInterface dialog, int which) {  
                            Toast.makeText(getApplicationContext(), "确认",  
                                    Toast.LENGTH_LONG).show();  
                        }  
                    }).show();       
        }  
          
          
         //第七种 对话框 自定义的对话框  
        public void myDialog()
{  
            LayoutInflater  layoutInflater = getLayoutInflater();  
            View view = layoutInflater.inflate(R.layout.activity_main, null); //R.layout.activty_main自定义的布局文件  
            new AlertDialog.Builder(this).setView(view).setTitle("自定义的对话框").setPositiveButton("确认按钮", new DialogInterface.OnClickListener() {  
                  
                @Override  
                public void onClick(DialogInterface dialog, int which) {  
                    //处理  
                      
                }  
            }).show();  
        }  
          
          
        //第八种 对话框 进度条对话框  
        public void test8(){  
            handler = new Handler() {  
      
                @Override  
                public void handleMessage(Message msg) {  
      
                    super.handleMessage(msg);  
                    switch (msg.what) {  
                    case PRO:  
      
                        if (progress >= MAX_PROGRESS) {  
                            // 重新设置  
                            progress = 0;  
                            progressDialog.dismiss();// 销毁对话框  
                        } else {  
                            progress++;  
                            progressDialog.incrementProgressBy(1);  
      
                            // 延迟发送消息  
                            handler.sendEmptyMessageDelayed(PRO, 100);  
                        }  
                        break;  
                    default:  
                        break;  
                    }  
      
                }  
            };  
      
            progressDialog = new ProgressDialog(this);  
            progressDialog.setIcon(R.drawable.ic_launcher);  
            progressDialog.setTitle("正在处理数据......");  
            progressDialog.setMessage("请稍后...");  
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条对话框  
                                                                                // 样式(水平,旋体)  
      
            // 进度最大值  
            progressDialog.setMax(MAX_PROGRESS);  
      
            progressDialog.setButton("暂停", new DialogInterface.OnClickListener() {  
      
                @Override  
                public void onClick(DialogInterface dialog, int which) {  
                    //删除消息队列  
                    handler.removeMessages(PRO);  
      
                }  
            });  
      
            progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {  
      
                @Override  
                public void onClick(DialogInterface dialog, int which) {  
      
                    //删除消息队列  
                    handler.removeMessages(PRO);  
                    //恢复进度初始值  
                    progress=0;  
                    progressDialog.setProgress(progress);  
                }  
            });  
      
            // 显示  
            progressDialog.show();  
              
            //必须设置到show之后  show之前 可能bug  
            progress = (progress>0)?progress:0;  
            progressDialog.setProgress(progress);  
              
            // 线程  
            handler.sendEmptyMessage(PRO);  
        }  
    }  
原文地址:https://www.cnblogs.com/maxma/p/9169633.html