对话框和自定义对话框

对话框

普通对话框

//构建者
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                //设置属性
                builder.setTitle("标题");
                builder.setIcon(R.drawable.a);
                builder.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();

单选对话框

 AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
                builder1.setTitle("请选择");
                //设置确定和取消按钮
                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
                    }
                });
                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "点击了取消 ", Toast.LENGTH_SHORT).show();
                    }
                });
                final String[] strs={"鸡蛋","鸭蛋","卤蛋 "};
                builder1.setSingleChoiceItems(strs, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, strs[which], Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog alertDialog1 = builder1.create();
                alertDialog1.show();

多选框

AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
                builder2.setTitle("这是一道多选题");
                final String[] items={"1","2","3","4"};
                boolean[] flags={true,false,false,false};
                //设置确定和取消按钮
                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();
                    }
                });
                builder2.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked){
                            Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
                        }

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

自定义对话框

final AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
                View inflate = LayoutInflater.from(this).inflate(R.layout.coustom_one, null);
                View viewById = inflate.findViewById(R.id.img);

                builder3.setView(inflate);
                final AlertDialog alertDialog3 = builder3.create();

                viewById.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            alertDialog3.dismiss();
                    }
                });
                alertDialog3.show();

水平进度条对话框

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);

圆形模糊进度对话框

 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);

日期选择对话框

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)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();

时间选择对话框

//日历对象
                Calendar calendar1 = 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();
                    }
                },calendar1.get(Calendar.HOUR),calendar1.get(Calendar.MINUTE),false).show();

对话框

创建一个继承Dialog的类

public class MyDialog extends Dialog {
    private TextView title;
    private TextView message;
    private Button no;
    private Button yes;

    private String Mytitle;
    private String Mymessage;
    private String Myno;
    private String Myyes;

    //------------------------------------------------------
    //2、写一个接口类型的成员属性
    private YesOnClick yesOnClick;
    //1、定义一个点击 事件 的接口
    public interface YesOnClick{
        void onClick();
    }
    //3、提供一个set方法
    public void setYesOnClick(YesOnClick yesOnClick) {
        this.yesOnClick = yesOnClick;
    }
    //------------------------------------------------------

    //定义一个点击 事件 的接口
    public interface NoOnClick{
        void onClick();
    }
    private NoOnClick noOnClick;

    public void setNoOnClick(NoOnClick noOnClick) {
        this.noOnClick = noOnClick;
    }
    //------------------------------------------------------
    public MyDialog(@NonNull Context context) {
        super(context);
    }

    public void setMymessage(String mymessage) {
        Mymessage = mymessage;
    }

    public void setMyno(String myno) {
        Myno = myno;
    }

    public void setMyyes(String myyes) {
        Myyes = myyes;
    }

    //------------------------------------------------------

    public void setMytitle(String mytitle) {
        Mytitle = mytitle;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_one);

        title = (TextView) findViewById(R.id.title);
        if (Mytitle != null){
            title.setText(Mytitle);
        }
        message = (TextView) findViewById(R.id.message);
        if (Mymessage != null){
            message.setText(Mymessage);
        }
        //------------------------------------------------------
        no = (Button) findViewById(R.id.no);
        if (Myno != null){
            no.setText(Myno);
        }
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noOnClick.onClick();
                dismiss();
            }
        });
        //------------------------------------------------------
        yes = (Button) findViewById(R.id.yes);
        if (Myyes != null){
            yes.setText(Myyes);
        }
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesOnClick.onClick();
                dismiss();
            }
        });
    }
}

操作

 MyDialog myDialog = new MyDialog(this);
                myDialog.setMytitle("卤蛋头");
                myDialog.setMymessage("想学腌制卤蛋的技巧么?");
                myDialog.setMyyes("想");
                myDialog.setMyno("不想");
                myDialog.setNoOnClick(new MyDialog.NoOnClick() {
                    @Override
                    public void onClick() {
                        Toast.makeText(MainActivity.this, "回答想", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog.setYesOnClick(new MyDialog.YesOnClick() {
                    @Override
                    public void onClick() {
                        Toast.makeText(MainActivity.this, "想也不教", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog.show();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值