第一天 各种对话框

第一天 各种对话框

    • 普通对话框
    • 单选对话框
    • 多选对话框
    • 简单自定义对话框![在这里插入图片描述](https://img-blog.csdnimg.cn/2019080220370478.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDk2NTg2Nw==,size_16,color_FFFFFF,t_70)
    • 水平进度条对话框
    • 时间对话框
    • 日期对话框
    • 完全自定义对话框
  • 1.思路:
  • 2.代码

普通对话框

第一天 各种对话框_第1张图片

  //创建建造者
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                //设置属性
                builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("删除")
                        .setMessage("确定删除吗?");
//               设置确定按钮
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "删除", Toast.LENGTH_SHORT).show();
                    }
                });
                //设置取消按钮
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                //使用建造者 创建对话框
                AlertDialog alertDialog = builder.create();
                //显示
                alertDialog.show();

单选对话框

第一天 各种对话框_第2张图片

 AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
                builder1.setPositiveButton("选择", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "111", Toast.LENGTH_SHORT).show();
                    }
                });
                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "222", Toast.LENGTH_SHORT).show();
                    }
                });
                
                final String[] str=new String[]{"晴天","阴天","下雨天"};
                //设置单选列表
                //参数一 列表 参数二 默认选中下标 参数三 事件监听
                builder1.setSingleChoiceItems(str, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "你选中了"+str[which], Toast.LENGTH_SHORT).show();
                    }
                });
                //创建对话框
                AlertDialog alertDialog1 = builder1.create();
                //展示
                alertDialog1.show();

多选对话框

第一天 各种对话框_第3张图片

  final String[] items={"张三","李四","王五","赵六"};
                //选中状态
                final boolean[] flags={true,false,false,false};
                AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
                //确认
                builder2.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int i = 0; i 

简单自定义对话框第一天 各种对话框_第4张图片

  //创建建造者
                AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
                //布局加载器加载需要的布局
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.diy_layout, null);
                //找到布局控件
                ImageView imageView = inflate.findViewById(R.id.image_id);
                //设置自定义布局
                builder3.setView(inflate);
                final AlertDialog alertDialog3 = builder3.create();
                //展示
                alertDialog3.show();
                //为自定义的控件事件
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog3.dismiss();
                    }
                });

水平进度条对话框

第一天 各种对话框_第5张图片

      final ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setMessage("进度条");
                progressDialog.setMax(100);
                progressDialog.setProgress(0);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //定时器
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    int i=0;
                    @Override
                    public void run() {
                        //每秒增加20
                        i+=20;
                        if(i==100){
                            progressDialog.dismiss();
                            timer.cancel();
                        }
                    progressDialog.setProgress(i);
                    }
                },0,1000);
                //确定监听
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "11111", Toast.LENGTH_SHORT).show();
                    }
                });
                //取消监听
                progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                progressDialog.show();

时间对话框

第一天 各种对话框_第6张图片

   /**
                 * 上下文
                 * 监听
                 * 默认的年
                 * 默认的月
                 * 默认的日
                 */
                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();

日期对话框

第一天 各种对话框_第7张图片

    /**
                 * 上下文
                 * 监听器
                 * 默认的时
                 * 默认的分钟
                 * 是否采用24小时
                 */
                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();

完全自定义对话框

第一天 各种对话框_第8张图片

1.思路:

1.自定义类继承Dialog
2.重写Dialog的方法并添加需要的功能
3.在Activity中实例化自定义的对象

2.代码

第一天 各种对话框_第9张图片
1.自定义类继承dialog

public class diydialog extends Dialog{
    private TextView diyTitleId;
    private TextView diyMessageId;
    private Button quxiaoId;
    private Button quedingId;
    private String titlestr,messagestr,yewstr,nostr;
    private onNoOnclickListenter onNoOnclickListenter;
    private onYesOnclickListener onYesOnclickListener;

    public diydialog(Context context) {
        super(context);
    }
//取消的监听
    public void setOnNoOnclickListenter(String str,onNoOnclickListenter onNoOnclickListenter){
        if(str!=null){
            nostr=str;
        }
        this.onNoOnclickListenter=onNoOnclickListenter;
    }
    //确定的监听
    public void setOnYesOnclickListener(String str,onYesOnclickListener onYesOnclickListener){
        if(str!=null){
            yewstr=str;
        }
        this.onYesOnclickListener=onYesOnclickListener;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diy1_layout);

        setCanceledOnTouchOutside(false);
        diyTitleId = findViewById(R.id.diy_title_id);
        diyMessageId = findViewById(R.id.diy_message_id);
        quxiaoId = findViewById(R.id.quxiao_id);
        quedingId = findViewById(R.id.queding_id);
//  不为空时修改 为空不修改
        if(titlestr!=null){
        diyTitleId.setText(titlestr);
        }
        if(messagestr!=null){
            diyMessageId.setText(messagestr);
        }
        if(yewstr!=null){
            quedingId.setText(yewstr);
        }
        if(nostr!=null){
            quxiaoId.setText(nostr);
        }
        //设置 点击  对外的监听
        quxiaoId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onNoOnclickListenter!=null){
                    onYesOnclickListener.onYewClick();
                    dismiss();
                }
            }
        });
        quedingId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onYesOnclickListener!=null){
                    onYesOnclickListener.onYewClick();
                    dismiss();
                }
            }
        });
    }

//获取值
    public void setTitlestr(String titlestr) {
        this.titlestr = titlestr;
    }

    public void setMessagestr(String messagestr) {
        this.messagestr = messagestr;
    }

    public interface onNoOnclickListenter{
        public void onNoClick();
    }
    public interface onYesOnclickListener{
        public void onYewClick();
    }
}

2.MainActivity类

 diydialog diydialog = new diydialog(this);
                diydialog.setTitlestr("哈哈哈哈哈");
                diydialog.setMessagestr("我是消息");
                diydialog.setOnNoOnclickListenter("no", new diydialog.onNoOnclickListenter() {
                    @Override
                    public void onNoClick() {

                    }
                });
                diydialog.setOnYesOnclickListener("yes", new diydialog.onYesOnclickListener() {
                    @Override
                    public void onYewClick() {
                        Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
                    }
                });
                diydialog.show();

你可能感兴趣的:(android,第一月)