Android 七种对话框使用

Android 七种对话框使用

  1. 简单对话框
  2. 简单列表对话框
  3. 单选列表对话框
  4. 多选列表对话框
  5. 进度条对话框
  6. 编辑对话框
  7. 自定义对话框

简单对话框

    private void showNormalDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("简单对话框")
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("这是一个简单对话框")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //定义自己想要做的操作
                        showText("确定");
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText("取消");
            }
        });
        builder.create().show();
    }

简单列表对话框

    private void showListDialog() {
        final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"};
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("简单列表对话框");
        //千万不要加这句,不然列表显示不出来
//        builder.setMessage("这是一个简单的列表对话框");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText(items[which]);
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText("确定");
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText("取消");
            }
        });

        builder.create().show();
    }

单选列表对话框

    private void showSingleChoiceDialog() {
        final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"};
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("简单列表对话框");
        //千万不要加这句,不然列表显示不出来
//        builder.setMessage("这是一个简单的列表对话框");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText(items[which]);
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText("确定");
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText("取消");
            }
        });

        builder.create().show();
    }

多选列表对话框

    private void showMultiChoiceDialog() {
        final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"};
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("简单列表对话框");
        //千万不要加这句,不然列表显示不出来
//        builder.setMessage("这是一个简单的列表对话框");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setMultiChoiceItems(items, new boolean[]{false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                showText(items[which]);
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText("确定");
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showText("取消");
            }
        });

        builder.create().show();
    }

进度条对话框

    private void showProgressDialog() {
        final ProgressDialog dialog = new ProgressDialog(this);
        final int MAX_PROGRESS = 100;
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setIcon(R.mipmap.ic_launcher);
        dialog.setTitle("提示");
        dialog.setMessage("这是一个进度条对话框");
        dialog.setProgress(0);
        //监听取消时间
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                showText("进度条结束");
            }
        });
        dialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress = 0;
                while (progress < MAX_PROGRESS) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress++;
                    //非UI线程,可以处理DialogUI
                    dialog.setProgress(progress);
                }
                dialog.cancel();
            }
        }).start();

    }

编辑对话框

    private void showEditDialog() {
        final EditText editText = new EditText(this);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("编辑对话框");
        builder.setMessage("这是一个编辑对话框");
        builder.setView(editText);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String input = editText.getText().toString();
                showText(input);
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }

自定义对话框


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="299dip"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="提示"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:gravity="center_vertical"
            android:paddingLeft="20.0dip"
            android:textSize="16dip"
            android:background="#288DF5"
            android:textColor="@color/white"/>
    LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:gravity="left|center"
            android:paddingLeft="20dip"
            android:paddingRight="20dip"
            android:textColor="@color/black"
            android:text="@string/custom_dialog_msg"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dip">

            <Button
                android:layout_width="fill_parent"
                android:layout_height="40dip"
                android:layout_weight="1"
                android:text="确定"
                android:textColor="@color/white"
                android:textSize="16sp"/>
            <Button
                android:layout_width="fill_parent"
                android:layout_height="40dip"
                android:layout_marginLeft="5dip"
                android:layout_weight="1"
                android:text="取消"
                android:textColor="@color/white"
                android:textSize="16sp"/>
        LinearLayout>
    LinearLayout>

LinearLayout>
    private void showCustomDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_dialog, null);
        builder.setView(layout).create().show();

    }

代码地址

你可能感兴趣的:(Android)