各种dialog的基本设置

一、确定取消选择对话框

  // 普通对话框
    public void simple_dialog(View view) {
        //构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置属性
        builder.setIcon(R.mipmap.ic_launcher_round);
        builder.setTitle("普通对话框");
        builder.setMessage("我是来自远方的你");
        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, "你点击了cancel", Toast.LENGTH_SHORT).show();
            }
        });
       
        AlertDialog dialog = builder.create();
        dialog.show();

    }

在这里插入图片描述

二、单选对话框

 // 单选对话框
    public void simple_dialog(View view) {
        //构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置属性
        builder.setIcon(R.mipmap.ic_launcher_round);
        builder.setTitle("单选对话框");
        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, "你点击了cancel", Toast.LENGTH_SHORT).show();
            }
        });
        //设置单选列表参数
        final String[] items = {"0-16岁","17-22岁","22岁以上"};
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "石某某的年龄是"+items[which], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

    }

在这里插入图片描述

三、多选对话框

 //多选对话框
    public void mulite_dialog(View view) {
		//设置多选的参数列表
        String[] items = {"张三","李四","王五","赵六"};
        //设置多选的默认初始选项
        final boolean[] flags = {true,true,true,false};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher_round);
        builder.setTitle("多选对话框");
        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, "你点击了cancel", Toast.LENGTH_SHORT).show();
            }
        });
        //参数一 选项的列表,参数二 默认的选项
        builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
               flags[which] = isChecked;
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();

    }

在这里插入图片描述

四、自定义对话框

// 自定义对话框
    public void customer_dialog(View view) {
        //获得试图
        View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.customer_dialog, null);
        ImageView imageView = view1.findViewById(R.id.iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view1);
        AlertDialog dialog = builder.create();
        dialog.show();

    }

在这里插入图片描述

五、水平进度条对话框

 //水平进度条对话框
    public void hor_progress_dialog(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("正在下载……");
        progressDialog.setMax(100);
        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+=5);
            }
        },0,1000);
    }

在这里插入图片描述

六、圆形水平进度条对话框

    public void square_progress_dialog(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        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);
    }

在这里插入图片描述

七、日期选择对话框

  //日期选择对话框
    public void date_choose_dialog(View view) {
        Calendar calendar = 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();
            }
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

在这里插入图片描述

八、时间选择对话框

  //时间选择对话框
    public void time_choose_dialog(View view) {
        Calendar instance = 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();
            }
        },instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),true).show();
    }

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android诚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值