Android中各种dialog

121 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

第一种:最普通的dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.drawable.follow);
                builder.setTitle("普通对话框:提示");
                builder.setMessage("是否要删除?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface pDialogInterface, int pI) {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface pDialogInterface, int pI) {
                        Toast.makeText(MainActivity.this, "忽略", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface pDialogInterface, int pI) {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.create().show();

第二种:列表dialog

 final String[] name = {"土豆","豆角","西红柿"};
                AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
                listDialog.setIcon(R.drawable.follow)
                        .setTitle("蔬菜列表")
                        .setItems(name, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface pDialogInterface, int pI) {
                                Toast.makeText(MainActivity.this, name[pI]+"", Toast.LENGTH_SHORT).show();
                            }
                        });
                listDialog.show();

第三种:单选dialog

final String[] names = {"土豆","豆角","西红柿"};
                AlertDialog.Builder singleSelect = new AlertDialog.Builder(this);
                singleSelect.setIcon(R.drawable.follow)
                        .setTitle("蔬菜单选列表")
                        .setSingleChoiceItems(names, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface pDialogInterface, int pI) {
                                Toast.makeText(MainActivity.this, names[pI]+"", Toast.LENGTH_SHORT).show();
                            }
                        });
                singleSelect.show();

第四种:多选dialog

final String[] food = {"土豆","豆角","西红柿"};
                final ArrayList<Integer> integers = new ArrayList<>();
                boolean isSelect[] = {false,false,false};
                integers.clear();
                AlertDialog.Builder moreSelect = new AlertDialog.Builder(this);
                moreSelect.setIcon(R.drawable.follow)
                        .setTitle("蔬菜多选列表")
                        .setMultiChoiceItems(food, isSelect, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface pDialogInterface, int pI, boolean pB) {
                                if(pB){
                                    integers.add(pI);
                                }else{
                                    integers.remove(pI);
                                }
                            }
                        });
                moreSelect.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface pDialogInterface, int pI) {
                        int size = integers.size();
                        String str = "";
                        for (int i = 0; i < size; i++) {
                            str += food[integers.get(i)]+"\n";
                        }
                        Toast.makeText(MainActivity.this, "选中了:"+str, Toast.LENGTH_SHORT).show();
                    }
                });
                moreSelect.show();

四种dialog效果图

第一种:普通dialog

1649987192(1).jpg

第二种:列表dialog

1649987268(1).png

第三种:单选dialog

1649987412(1).png

第四种:多选dialog

1649987362(1).png

再一次需求中要求写各种dialog,为了方便以后在遇到这样的需求,特此总结记录一下!如果以后再有其他样式的dialog,将会继续更新。