Dialog的使用2018-08-12

Dialog 是android 原生的弹出框一共有7种吧(尾部有原生dialog的github地址)

首先如果要看的的话先看看有没有你需要的效果(新手们)


Dialog的使用2018-08-12_第1张图片

第一个  首先我们来看一下基础的dialog (特性dialog如果头部要加图片必须有title才可以)

效果图


Dialog的使用2018-08-12_第2张图片
基础的

dialog1 =new AlertDialog.Builder(this)

.setTitle("这是标题")

//图片

        .setIcon(R.mipmap.ic_launcher)

//内容区

        .setMessage("这是内容区")

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();

            }

}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();

            }

}).setNeutralButton("忽略", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "你点击了忽略", Toast.LENGTH_SHORT).show();

            }

})

.create();


这个是自定义的dialog

看看效果图


Dialog的使用2018-08-12_第3张图片


dialog2=new AlertDialog.Builder(this)

.setTitle("标题")

.setIcon(R.mipmap.ic_launcher)

.setView(view)

.setMessage("你好呀")

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();

            }

})

.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();

            }

}).setNeutralButton("忽略", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "你点击了忽略", Toast.LENGTH_SHORT).show();

            }

})

.create();


简单列表对话框

效果图


Dialog的使用2018-08-12_第4张图片

dialog3 =new AlertDialog.Builder(this)

.setTitle("这是标题")

.setItems(items, new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();

            }

})

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();

            }

})

.setNeutralButton("取消", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();

            }

})

.create();

单选对话框

效果图

Dialog的使用2018-08-12_第5张图片

final int[] count = {0};

dialog4 =new AlertDialog.Builder(this)

.setTitle("标题")

.setIcon(R.mipmap.ic_launcher)

.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

count[0] = which;

            }

})

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, items[count[0]], Toast.LENGTH_SHORT).show();

            }

})

.setNeutralButton("取消", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();

            }

})

.create();

多线Dialog 对话框


Dialog的使用2018-08-12_第6张图片

dialog5 =new AlertDialog.Builder(this)

.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

Toast.makeText(MainActivity.this, items[which] + isChecked, Toast.LENGTH_SHORT).show();

            }

}).setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

StringBuffer sb =new StringBuffer();

                for (int i =0; i

if (selected[i] ==true){

sb.append(items[i]);

                    }

}

Toast.makeText(MainActivity.this, sb, Toast.LENGTH_SHORT).show();

            }

}).setNeutralButton("取消", new DialogInterface.OnClickListener() {

@Override

            public void onClick(DialogInterface dialog, int which) {

Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();

            }

})

.create();

后面待更新

github地址  https://github.com/1037438704/MyListDialog

你可能感兴趣的:(Dialog的使用2018-08-12)