1.基本使用流程
Step 1:创建AlertDialog.Builder对象;
Step 2:调用setIcon()设置图标,setTitle()或setCustomTitle()设置标题;
Step 3:设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
Step 4:调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;
Step 5:调用create()方法创建这个对象,再调用show()方法将对话框显示出来;



类型

实现方法

注释

普通Dialog

final AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage("真的删除该联系人吗?").setPositiveButton("是",new DialogInterface.OnClickListener(){                    public void onClick(DialogInterface dialog,int which){
                        textView.setText("成功删除");
                    }
                }).setNegativeButton("否",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int which){
                        textView.setText("取消删除");
                    }
                });
                AlertDialog ad=builder.create();
                ad.show();

 

多按钮

Dialog

final AlertDialog.Builder builder=new AlertDialog.Builder(this);              
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.setTitle("多按钮Dialog").setPositiveButton("按钮一", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "按钮一", Toast.LENGTH_SHORT).show();
                    }
                }).setNeutralButton("按钮二", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "按钮二", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("按钮三",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int which){
                        Toast.makeText(MainActivity.this,"按钮三",Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog ad=builder.create();
                ad.show();
            }
        });

   只是比上一种按钮多了setNeutralButton,具体方法不变

列表

Dialog

 

final String[] mList={"选项一","选项二","选项三","选项四","选项五"};
         AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
         builder.setTitle("列表对话框");
         builder.setItems(mList,new DialogInterface.OnClickListener(){
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 /**
                  * 小标从0开始
                  */
                 Toast.makeText(MainActivity.this,"选项"+which,Toast.LENGTH_SHORT).show();
             }
         });AlertDialog ad=builder.create();
                 ad.show();}});

  /**
                 * 其中which是

小标从0开始
                 */

单项选择

Dialog

final String[] kList={"选项一","选项二","选项三","选项四","选项五"};
     final AlertDialog.Builder builder4=new AlertDialog.Builder(MainActivity.this);
 button4.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         builder4.setTitle("单项选择对话框");
         builder4.setSingleChoiceItems(kList, 0, new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 yourChose = which ;
             }
         });
         builder4.setPositiveButton("确定",new DialogInterface.OnClickListener(){
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 if(yourChose!=-1)
                     Toast.makeText(MainActivity.this,kList[yourChose],Toast.LENGTH_SHORT).show();
                     
             }
         });
 AlertDialog ad=builder4.create();
         ad.show();


     }


 });

 

setSingleChoiceItems()有四个重载的方法:
 1、从资源文件中装载数据:
 public AlertDialog.Builder setSingleChoiceItems(int itemsId, int checkedItem, final OnClickListener listener)
 2、从数据集中装载数据
 public AlertDialog.Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, final OnClickListener listener)
 3、从字符串数组中装载数据
 public AlertDialog.Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)
 4、从ListAdapter对象中装载数据
 public AlertDialog.Builder setSingleChoiceItems(ListAdapter adpater, int checkedItem, final OnClickListener listener)


 我们只需要关注第二个参数:
 checkedItem:指定哪个项目被选中,默认为0表示选中第一个项目,-1表示没有项目被选中

多项选择

Dialog

ArrayList<Integer>myChose=new ArrayList<Integer>();


  final String[] pList = {"选项一", "选项二", "选项三", "选项四", "选项五"};
         final boolean mChoseSts[] = {false, false, false, false, false};
         myChose.clear();
         final AlertDialog.Builder builder5=new AlertDialog.Builder(MainActivity.this);
         button5.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {


                 builder5.setTitle("多项选择对话框").setMultiChoiceItems(pList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                         mChoseSts[which]=isChecked;
                     }
                 });
                 builder5.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         String str = "";
                         for (int i = 0; i < mChoseSts.length; i++) {
                             if(mChoseSts[i])
                             str+=pList[i];
                         }
                         Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                     }
                 });
 AlertDialog ad=builder5.create();
                 ad.show();
             }
         });

 




setSingleChoiceItems()有四个重载的方法:
1、从资源文件中装载数据:
public AlertDialog.Builder setSingleChoiceItems(int itemsId, int checkedItem, final OnClickListener listener)
2、从数据集中装载数据
public AlertDialog.Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, final OnClickListener listener)
3、从字符串数组中装载数据
public AlertDialog.Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)
4、从ListAdapter对象中装载数据
public AlertDialog.Builder setSingleChoiceItems(ListAdapter adpater, int checkedItem, final OnClickListener listener)


我们只需要关注第二个参数:
checkedItem:指定哪个项目被选中,默认为0表示选中第一个项目,-1表示没有项目被选中


比如当为0时


android 输入对话框 安卓普通对话框代码_android

当为-1时

android 输入对话框 安卓普通对话框代码_数据_02






package com.example.administrator.test;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public  static AnimationDrawable animationDrawable;
    public int yourChose=-1;
    ArrayList<Integer>myChose=new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Button button1,button2,button3,button4,button5;
        final TextView textView;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        button3=(Button)findViewById(R.id.button3);
        button4=(Button)findViewById(R.id.button4);
        button5=(Button)findViewById(R.id.button5);
        textView=(TextView)findViewById(R.id.textView);
        /**
         * 普通按钮实现
         */
        final AlertDialog.Builder builder1=new AlertDialog.Builder(this);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder1.setMessage("真的删除该联系人吗?").setPositiveButton("是",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int which){
                        textView.setText("成功删除");
                    }
                }).setNegativeButton("否",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int which){
                        textView.setText("取消删除");
                    }
                });
                AlertDialog ad=builder1.create();
                ad.show();
            }
        });

        /**
         * 多按钮实现
         */
        final AlertDialog.Builder builder2=new AlertDialog.Builder(this);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder2.setTitle("多按钮Dialog").setPositiveButton("按钮一", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "按钮一", Toast.LENGTH_SHORT).show();
                    }
                }).setNeutralButton("按钮二", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "按钮二", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("按钮三", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "按钮三", Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog ad=builder2.create();
                ad.show();
            }
        });
        /**
         * 列表对话框实现
         */
        final String[] mList={"选项一","选项二","选项三","选项四","选项五"};
        final AlertDialog.Builder builder3=new AlertDialog.Builder(MainActivity.this);
       button3.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               builder3.setTitle("列表对话框");
               builder3.setItems(mList, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       /**
                        * 小标从0开始
                        */

                       Toast.makeText(MainActivity.this, mList[which], Toast.LENGTH_SHORT).show();
                   }
               });
               AlertDialog ad = builder3.create();
               ad.show();
           }
       });

/**
 * 单项选择实现
 */


        final String[] kList={"选项一","选项二","选项三","选项四","选项五"};
    final AlertDialog.Builder builder4=new AlertDialog.Builder(MainActivity.this);
button4.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        builder4.setTitle("单项选择对话框");
        builder4.setSingleChoiceItems(kList, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                yourChose = which;
            }
        });
        builder4.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (yourChose != -1)
                    Toast.makeText(MainActivity.this, kList[yourChose], Toast.LENGTH_SHORT).show();

            }
        });
AlertDialog ad=builder4.create();
        ad.show();
    }

});
        /**
         * 多项选择的实现
         */
        final String[] pList = {"选项一", "选项二", "选项三", "选项四", "选项五"};
        final boolean mChoseSts[] = {false, false, false, false, false};
        myChose.clear();
        final AlertDialog.Builder builder5=new AlertDialog.Builder(MainActivity.this);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                builder5.setTitle("多项选择对话框").setMultiChoiceItems(pList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        mChoseSts[which]=isChecked;
                    }
                });
                builder5.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String str = "";
                        for (int i = 0; i < mChoseSts.length; i++) {
                            if(mChoseSts[i])
                            str+=pList[i];
                        }
                        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                    }
                });
AlertDialog ad=builder5.create();
                ad.show();
            }
        });

    }
}




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:weightSum="1">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通Dialog"
        android:id="@+id/button1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多按钮Dialog"
        android:id="@+id/button2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="列表Dialog"
        android:id="@+id/button3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单项选择Dialog"
        android:id="@+id/button4"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多项选择Dialog"
        android:id="@+id/button5"/>
</LinearLayout>




最终效果:



android 输入对话框 安卓普通对话框代码_android_03