Dialog八种对话框

Dialog对话框

一.常用对话框

1.普通对话框

2.单选对话框

3.多选对话框

4.自定义对话框

5.水平进度条对话框

6.圆形进度条对话框

7.日期选择对话框

8.时间选择对话框

一.常用的对话框

对话框 特殊
普通 AlertDialog.Builder()
单选 AlertDialog.Builder() setSingleChoiceItems()
多选 AlertDialog.Builder() setMultiChoiceItems()
日期 DatePickerDialog new DatePickerDialog(context,DatePickerDialog.OnDateSetListener,year,month,day)
时间 TimePickerDialog new TimePickerDialog(context, TimePickerDialog.OnTimeSetListener,时,分,是否24进制)
水平 ProgressDialog setStyle(ProgressDialog.STYLE_HORIZONTAL)
圆圈 ProgressDialog setStyle(ProgressDialog.STYLE_SPINNER)
自定义 AlertDialog.Builder() setView()

1.普通对话框

在这里插入图片描述


AlertDialog.Builder builder = new AlertDialog.Builder(this);//创建构造者
builder.setIcon(R.mipmap.zx);//设置图标
builder.setTitle("大帅哥");       //设置标题
builder.setMessage("我是1702C申德丰");   //设置内容
//设置确定按钮
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
    
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    
    
        Toast.makeText(MainActivity.this, "点击了ok", Toast.LENGTH_SHORT).show();
    }
});
//设置取消按钮
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
    
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    
    
        Toast.makeText(MainActivity.this, "点击取消了", Toast.LENGTH_SHORT);
    }
});
AlertDialog alertDialog = builder.create();
alertDialog.show();

2.单选按钮对话框

在这里插入图片描述

 AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
 builder2.setTitle("我是最帅的吗");
 builder2.setPositiveButton("好的", new DialogInterface.OnClickListener() {
    
    
     @Override
     public void onClick(DialogInterface dialog, int which) {
    
    
         Toast.makeText(MainActivity.this, "点击了好的", Toast.LENGTH_SHORT).show();
     }
 });

 builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    
     @Override
     public void onClick(DialogInterface dialog, int which) {
    
    
         Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
     }
 });

final String[] s=new String []{
    
    "帕加尼","布加迪威龙","法拉利拉法"};

 builder2.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
    
    
     @Override
     public void onClick(DialogInterface dialog, int which) {
    
    
         Toast.makeText(MainActivity.this, "选中"+s[which], Toast.LENGTH_SHORT).show();
     }
 });

 AlertDialog alertDialog2 = builder2.create();
 alertDialog2.show();

3.多选对话框

在这里插入图片描述

final String[] items={
    
    "申德丰","王鹏","张泽涛","杨子豪","常帅"};
final boolean[] flags=new boolean[]{
    
    true,true,true,true,false};

AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
builder3.setIcon(R.mipmap.zx);
builder3.setTitle("通讯人名");
builder3.setPositiveButton("yes", new DialogInterface.OnClickListener() {
    
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    
    
        for(int i=0;i<flags.length;i++){
    
    
            if(flags[i]){
    
    
                Toast.makeText(MainActivity.this, "这个人是:"+items[i], Toast.LENGTH_SHORT).show();
            }

        }
    }
});
builder3.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    
    
        Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
    }
});

builder3.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
    
    
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    
    
        flags[which] = isChecked;
    }
});
AlertDialog alertDialog3 = builder3.create();
alertDialog3.show();

4.自定义对话框

在这里插入图片描述

自定义布局
在这里插入图片描述

 View view= LayoutInflater.from(this).inflate(R.layout.layout1,null);
                AlertDialog.Builder builder1 = new AlertDialog.Builder(this);//创建构造者
                builder1.setIcon(R.mipmap.zx);//设置图标
                builder1.setTitle("大帅哥");       //设置标题
                //设置确定按钮
//                builder1.setPositiveButton("ok", new DialogInterface.OnClickListener() {
    
    
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
    
    
//                        Toast.makeText(MainActivity.this, "点击了ok", Toast.LENGTH_SHORT).show();
//                    }
//                });
//                //设置取消按钮
//                builder1.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
    
    
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
    
    
//                        Toast.makeText(MainActivity.this, "点击取消了", Toast.LENGTH_SHORT);
//                    }
//                });
                Button button = view.findViewById(R.id.btn_close);
                button.setOnClickListener(new View.OnClickListener() {
    
    
                    @Override
                    public void onClick(View v) {
    
    
                        alertDialog1.dismiss();
                    }
                });


                builder1.setView(view);
                alertDialog1 = builder1.create();
                alertDialog1.show();

5.水平进度条对话框

在这里插入图片描述

final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 progressDialog.setMax(100);
 progressDialog.setMessage("正在飞速下载");
 progressDialog.show();

final Timer timer = new Timer();
timer.schedule(new TimerTask() {
    
    
    int progress=0;
    @Override
    public void run() {
    
    
        if(progress==100){
    
    
            progressDialog.dismiss();
            timer.cancel();

        }
        progressDialog.setProgress(progress+=20);

    }
},0,1000);

6.圆形进度条对话框

在这里插入图片描述

final ProgressDialog progressDialog1 = new ProgressDialog(this);
progressDialog1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog1.setMax(100);
progressDialog1.setMessage("正在飞速下载");
progressDialog1.show();

final Timer timer1 = new Timer();
timer1.schedule(new TimerTask() {
    
    
    int progress=0;
    @Override
    public void run() {
    
    
        if(progress==100){
    
    
            progressDialog1.dismiss();
            timer1.cancel();

        }
        progressDialog1.setProgress(progress+=20);

    }
},0,1000);

7.日期选择对话框

在这里插入图片描述

Calendar instance = Calendar.getInstance();
    new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    
    
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    
    
            Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
        }
    },instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();

8.时间选择对话框

在这里插入图片描述

Calendar instance1 = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
    
    
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    
    
        Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
    }
},instance1.get(Calendar.HOUR),instance1.get(Calendar.MINUTE),true).show();

猜你喜欢

转载自blog.csdn.net/weixin_43841463/article/details/90953384