原CSDN博客已弃用,文章会逐渐迁移过来。

一、简单的入门对话框

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    android:padding="10dp">  
    <Button  
        android:id="@+id/btn1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="普通对话框1"  
        android:onClick="onClick"  
        />  
    <Button  
        android:id="@+id/btn2"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="普通对话框2"  
        android:onClick="onClick"  
        />  
</LinearLayout>  

MainActivity.java

`
package com.administrator.dialogtest;

import android.app.AlertDialog;  
import android.content.DialogInterface;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.Toast;  

public class MainActivity extends AppCompatActivity implements View.OnClickListener{  

    private Button btn1;  
    private Button btn2;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Button btn = findViewById(R.id.btn1);  
        Button btn2 = findViewById(R.id.btn2);  
    }  

    @Override  
    public void onClick(View v) {  
        switch (v.getId()) {  
            case R.id.btn1:  
                showNormalDialog();  
                break;  
            case R.id.btn2:  
                showNormalDialog2();  
        }  
    }  

    private void showNormalDialog2() {  
        AlertDialog dialog = new AlertDialog.Builder(this).create();  
        dialog.setTitle("提示");//这个是无返回值方法,所以不能像第一个按钮那样一直.set下去  
        dialog.setMessage("请为本次课堂打分");  
        //5 3 1  
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "1分", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                Toast.makeText(MainActivity.this,"您选择了:5分",Toast.LENGTH_SHORT).show();  
            }  
        });  
        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "5分", new DialogInterface.OnClickListener() {//这个不能传null,因为null不知道是空的Message对象还是空的OnClickListener对象  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  

            }  
        });  
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "3分", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  

            }  
        });  
        //一定要调用show方法,否则对话框不展示  
        dialog.show();  
    }  

    private void showNormalDialog() {  
        //AlertDialog  
        //AlerDialog的构造方法是被修饰为protected  
        //因此包外是无法访问的,所以我们利用构造器  
//        AlertDialog dialog = new AlertDialog(this);//因为方法定义时候定义了受保护的,因此要使用构造器  
        AlertDialog.Builder builder = new AlertDialog.Builder(this);  
//        builder.setTitle("提示").setMessage("你好").setPositiveButton()//因为每个方法的返回值都是一个builder,所以可以这样写  
        //设置对话框的标题  
        builder.setTitle("提示");  
        //设置内容  
        builder.setMessage("你是否确定退出当前程序");  
        //设置按钮  
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int which) {  
                //退出当前程序  
                MainActivity.this.finish();  
            }  
        });  
        builder.setNegativeButton("取消",null);//可以像上面确定按钮这样传点击事件,什么都不给为null  
//        builder.setNeutralButton();  
        //创建对话框  
//        builder.create();  
        //对话框的展示方法  
        builder.show();//show()方法里面写了create()方法,因此不需要额外多写一次  
    }  
}  

二、列表对话框和单选对话框

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    android:padding="10dp">  
    <Button  
        android:id="@+id/btn1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="列表对话框"  
        android:onClick="onClick"  
        />  
    <Button  
        android:id="@+id/btn2"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="单选对话框"  
        android:onClick="onClick"  
        />  
</LinearLayout>  

MainActivity.java

    package com.administrator.dialogtest;  

import android.app.AlertDialog;  
import android.content.DialogInterface;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.Toast;  

public class MainActivity extends AppCompatActivity implements View.OnClickListener{  

    private Button btn1;  
    private Button btn2;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Button btn = findViewById(R.id.btn1);  
        Button btn2 = findViewById(R.id.btn2);  
    }  

    @Override  
    public void onClick(View v) {  
        switch (v.getId()) {  
            case R.id.btn1:  
                showListDialog();  
                break;  
            case R.id.btn2:  
                showSingleDialog();  
        }  
    }  

    /** 
     * 单选对话框 
     */  
//    int idx = 0;  
    private void showSingleDialog() {  
        final String[] stars={"老周","老牛","老孙","老","写不下去了"};  
        AlertDialog.Builder builder = new AlertDialog.Builder(this)  
                .setTitle("请选择你喜欢的明星")  
                //参数1:选项  
                //参数2:默认被选项,传某个选项的索引  
                //参数3:被选中时的事件  
                .setSingleChoiceItems(stars, 0, new DialogInterface.OnClickListener() {  
                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
//                        idx = which;  
                        Toast.makeText(MainActivity.this,"您最喜欢的明星是:"+ stars[which],Toast.LENGTH_SHORT).show();  
                    }  
//                }).setPositiveButton("确定",null);//如果需要点击某个按钮后消失,这样设置  
//                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {  
//                    @Override  
//                    public void onClick(DialogInterface dialog, int which) {//这里的which和上面的不是同一个,所以要解决上面的传递下来的问题,定义一个全局变量  
//                        Toast.makeText(MainActivity.this, "您选择了:" + stars[idx], Toast.LENGTH_SHORT).show();  
//                    }  
//                });  
                });  
        builder.show();  
    }  

    /** 
     * 列表对话框 
     */  
    private void showListDialog() {  
        final String[] items = {"我是1","我是2","我是3","我是4"};  
        AlertDialog.Builder builder = new AlertDialog.Builder(this)  
                .setTitle("请选择:")  
                //设置列表项  
                .setItems(items, new DialogInterface.OnClickListener() {  
                    @Override  
                    public void onClick(DialogInterface dialog, int which) {//参数2:被点击项的索引  
                        Toast.makeText(MainActivity.this, "您选择了:" + items[which], Toast.LENGTH_SHORT).show();  
                    }  
                });  
        builder.show();  
    }  
}  

三、多选对话框和等待对话框

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    android:padding="10dp">  
    <Button  
        android:id="@+id/btn1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="多选对话框"  
        android:onClick="onClick"  
        />  
    <Button  
        android:id="@+id/btn2"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="等待对话框"  
        android:onClick="onClick"  
        />  
</LinearLayout>  

MainActivity.java

    package com.administrator.dialogtest;  

import android.app.AlertDialog;  
import android.app.ProgressDialog;  
import android.content.DialogInterface;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.widget.Button;  
import android.widget.ProgressBar;  
import android.widget.Toast;  

public class MainActivity extends AppCompatActivity implements View.OnClickListener{  

    private Button btn1;  
    private Button btn2;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Button btn = findViewById(R.id.btn1);  
        Button btn2 = findViewById(R.id.btn2);  
    }  

    @Override  
    public void onClick(View v) {  
        switch (v.getId()) {  
            case R.id.btn1:  
                showMultiDialog();  
                break;  
            case R.id.btn2:  
                showWaitingDialog();  
        }  
    }  



    /** 
     * 多选对话框 
     */  
    private void showMultiDialog() {  
        final String[] sports = {"篮球","网球","乒乓球","曲棍球","滑板","游泳"};  
        final boolean[] checked = {true,false,true,true,false,false};  
        AlertDialog.Builder builder =new AlertDialog.Builder(this)  
                .setTitle("你最喜欢的运动")  
                //参数1:选项  
                //参数2:默认备选项(true:选中,false:未选择)  
                //参数3:被选中时触发的事项  
                .setMultiChoiceItems(sports,checked , new DialogInterface.OnMultiChoiceClickListener() {  
                            @Override  
                            //参数1:对话框本身  
                            //参数2:按钮的索引  
                            //参数3:标志按钮是否处于被选中true(选中)false(取消选中)  
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
                                //无论是选中还是取消选择,都会触发onclick方法  
                                //根据which数组下标,将isChecked的选中状态保存到数组checked  
//                                checked[which]=isChecked;//这个方法默认把这步做了。所以不需要再写  
                                Log.e("Log", sports[which]);  
                            }  
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {  
                            @Override  
                            public void onClick(DialogInterface dialog, int which) {  
                                String msg="你的爱好是:";  
                                for (int i = 0;i<checked.length;i++){  
                                    if(checked[i]){  
                                        msg += sports[i] + " ";//遍历刚才上面保存的选中状态的数组  
                                    }  
                                }  
                                Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();  
                            }  
                });  
//                });  
        builder.show();  
    }  

    /** 
     * 等待对话框 
     */  
    private void showWaitingDialog() {  
        //进度对话框,默认样式就是转圈  
        ProgressDialog dialog = new ProgressDialog(this);  
        dialog.setTitle("我是一个等待对话框");  
        dialog.setMessage("请等待...");  
//        dialog.setCancelable(false);//设置是否也取消,默认是true可取消  
//        dialog.dismiss();//设置对话框消失  
        dialog.show();  
    }  
}  

四、进度条对话框和输入对话框

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    android:padding="10dp">  
    <Button  
        android:id="@+id/btn1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="进度条对话框"  
        android:onClick="onClick"  
        />  
    <Button  
        android:id="@+id/btn2"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="输入对话框"  
        android:onClick="onClick"  
        />  
</LinearLayout>  

MainActivity.java

    package com.administrator.dialogtest;  

import android.app.AlertDialog;  
import android.app.ProgressDialog;  
import android.content.DialogInterface;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.ProgressBar;  
import android.widget.Toast;  

public class MainActivity extends AppCompatActivity implements View.OnClickListener{  

    private Button btn1;  
    private Button btn2;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Button btn = findViewById(R.id.btn1);  
        Button btn2 = findViewById(R.id.btn2);  
    }  

    @Override  
    public void onClick(View v) {  
        switch (v.getId()) {  
            case R.id.btn1:  
                showProgressDialog();  
                break;  
            case R.id.btn2:  
                showInputDialog();  
        }  
    }  

    /** 
     * 进度条对话框 
     */  
    private void showProgressDialog() {  
        final ProgressDialog dialog = new ProgressDialog(this);  
        dialog.setTitle("下载中...");  
        dialog.setMessage("请等待");  
        dialog.setIndeterminate(false);//设置进度条模糊,感觉进度条在动,true是循环动  
        //设置对话框的样式为水平样式  
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
//        dialog.setProgress(40);//设置进度  
        new Thread(){  
            public void run(){  
                super.run();  
                //让进度条从1到100运动  
                for(int i =1;i<=100;i++){  
                    dialog.setProgress(i);  
                    try {  
                        Thread.sleep(50);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
                dialog.dismiss();  
            }  
        }.start();  
        dialog.show();  
    }  

    /** 
     * 输入对话框 
     */  
    private void showInputDialog() {  
        final EditText editText = new EditText(this);  
        AlertDialog.Builder builder = new AlertDialog.Builder(this)  
                .setTitle("提示")  
                .setView(editText)//需要传一个布局或一个View视图,也可以在资源文件夹中新建一个布局引进来  
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
                    @Override  
                    public void onClick(DialogInterface dialog, int which) {  
                        Toast.makeText(MainActivity.this,"您输入的是:"+editText.getText().toString(),Toast.LENGTH_SHORT).show();  
                    }  
                });  
        builder.show();  
    }  
}