Android AlertDialog对话框使用(各种样式)

先上几张效果图
Android AlertDialog对话框使用(各种样式)
Android AlertDialog对话框使用(各种样式)
Android AlertDialog对话框使用(各种样式)

Android AlertDialog对话框使用(各种样式)

Android AlertDialog对话框使用(各种样式)

这是XML文件中的控件,只是使用了按钮点击来触发

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main3Activity">

    <Button
        android:id="@+id/btn_open_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="常见的模式" />

    <Button
        android:id="@+id/btn_open_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="列表对话框" />

    <Button
        android:id="@+id/btn_open_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="单选对话框" />

    <Button
        android:id="@+id/btn_open_four"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="多选对话框" />

    <Button
        android:id="@+id/btn_open_five"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="自定义的" />
</LinearLayout>

这是Activity中的代码

public class Main3Activity extends AppCompatActivity implements View.OnClickListener {
    private String[] citys = new String[]{"北京", "上海", "深圳", "香港"};
    private AlertDialog d;
    private AlertDialog dialog;
    private Button btn_open_one;
    private Button btn_open_two;
    private Button btn_open_three;
    private Button btn_open_four;
    private Button btn_open_five;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }
    private void initView() {
        btn_open_one = (Button) findViewById(R.id.btn_open_one);
        btn_open_two = (Button) findViewById(R.id.btn_open_two);
        btn_open_three = (Button) findViewById(R.id.btn_open_three);
        btn_open_four = (Button) findViewById(R.id.btn_open_four);
        btn_open_five = (Button) findViewById(R.id.btn_open_five);

        btn_open_one.setOnClickListener(this);
        btn_open_two.setOnClickListener(this);
        btn_open_three.setOnClickListener(this);
        btn_open_four.setOnClickListener(this);
        btn_open_five.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_open_one:
                //常见的模式
                opendialog();
                break;
            case R.id.btn_open_two:
                //列表对话框
                openlistdialog();
                break;
            case R.id.btn_open_three:
                //单选对话框
                opensingledialog();
                break;
            case R.id.btn_open_four:
                //多选对话框
                muldialog();
                break;
            case R.id.btn_open_five:
                //自定义的
                customDialog();
                break;
        }
    }

    //常见的
    public void opendialog() {
        // 1.创建builder对象
        AlertDialog.Builder builder = new AlertDialog.Builder(Main3Activity.this);

        //设置标题
        builder.setTitle("my first dialog");
        builder.setIcon(R.mipmap.ic_launcher);//设置标题图片
        //设置内容
        builder.setMessage("确定删除吗?");

        //设置按钮   Positive 积极的 正确面
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(Main3Activity.this, "ok", Toast.LENGTH_SHORT).show();
            }
        });
        //Negative 消极的
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(Main3Activity.this, "cancel", Toast.LENGTH_SHORT).show();
            }
        });

        //Neutral 一般的
        builder.setNeutralButton("一般", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(Main3Activity.this, "一般", Toast.LENGTH_SHORT).show();
            }
        });
        //2.调用 builder的create
        AlertDialog dialog = builder.create();
        //3.显示
        dialog.show();

    }

    //列表对话框
    public void openlistdialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("请选择城市:");
        builder.setItems(citys, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(Main3Activity.this, "当前条目:" + arg1, Toast.LENGTH_SHORT).show();
            }
        });
        //创建,显示
        AlertDialog d = builder.create();
        d.show();

    }

    //单选对话框
    public void opensingledialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择城市:");
        //设置单选  第二个参数是 默认选中项的下标
        builder.setSingleChoiceItems(citys, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(Main3Activity.this, "当前条目:" + arg1, Toast.LENGTH_SHORT).show();
                //d.dismiss();//关闭对话框
            }
        });
        //创建显示
        d = builder.create();
        d.show();

    }

    //多选对话框
    public void muldialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择城市:");
        //设置多选
        builder.setMultiChoiceItems(citys, new boolean[]{true, true, true, true}, new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
                // TODO Auto-generated method stub

            }
        });
        //创建,显示
        d = builder.create();
        d.show();
    }

    //自定义的
    public void customDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //加载布局
        View contentView = View.inflate(this, R.layout.dialog, null);
        //查找控件
        final EditText etName = (EditText) contentView.findViewById(R.id.et_name);
        final EditText etPwd = (EditText) contentView.findViewById(R.id.et_pwd);
        Button btnOk = (Button) contentView.findViewById(R.id.btn_ok);
        Button btnCancle = (Button) contentView.findViewById(R.id.btn_cancle);

        btnOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String name = etName.getText().toString();
                String pwd = etPwd.getText().toString();

                if (name.equals("") || pwd.equals("")) {
                    Toast.makeText(Main3Activity.this, "用户名或密码不能为空。", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
                    startActivity(intent);
                    //关闭对话框
                    dialog.dismiss();
                }
            }
        });
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //关闭对话框
                dialog.dismiss();
            }
        });
        //设置内容
        builder.setView(contentView);
        //创建
        dialog = builder.create();
        //显示
        dialog.show();
    }
}

这是自定义的Dialog的XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ccc"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定义的view对话框" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0f0"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名" />

            <EditText
                android:id="@+id/et_name"
                android:layout_width="100dp"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码" />

            <EditText
                android:id="@+id/et_pwd"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:password="true" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_cancle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="取消" />

            <Button
                android:id="@+id/btn_ok"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确定" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>