第一天 Dialog对话框

一. 基础8种

1. 普通对话框

在这里插入图片描述

 AlertDialog.Builder bd1 = new AlertDialog.Builder(this);

                bd1.setIcon(R.mipmap.ic_launcher);
                bd1.setTitle("这是普通对话框");
                bd1.setMessage("这个月必升!");

                bd1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "您点击了确定", Toast.LENGTH_SHORT).show();
                    }
                });

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

                AlertDialog dg1 = bd1.create();

                dg1.show();
2. 单选对话框

在这里插入图片描述

  AlertDialog.Builder bd2 = new AlertDialog.Builder(this);

                bd2.setIcon(R.mipmap.ic_launcher_round);
                bd2.setTitle("这是单选框");

                bd2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                });

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

                final String[] item = new String[]{"晓洁","媳妇","老婆","丫丫"};

                bd2.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "您选择了:"+item[which], Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog dg2 = bd2.create();

                dg2.show();
3. 多选对话框

在这里插入图片描述

				 // 创建数据源
                final String[] food = new String[]{"荔枝","桃子","草莓","蓝莓"};
                final boolean[] flag = new boolean[]{false,false,false,false};

                AlertDialog.Builder bd3 = new AlertDialog.Builder(this);

                bd3.setIcon(R.mipmap.ic_launcher_round);
                bd3.setTitle("这是多选框:");

                bd3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = new String();

                        for (int i = 0; i < flag.length; i++) {
                            if (flag[i]){
                                s = s+food[i]+"  ";
                            }
                        }
                        Toast.makeText(MainActivity.this, "您喜欢的水果是:"+s, Toast.LENGTH_SHORT).show();
                    }
                });

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

                bd3.setMultiChoiceItems(food, flag, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        flag[which] = isChecked;
                    }
                });

                AlertDialog dg3 = bd3.create();

                dg3.show();
4. 自定义对话框

在这里插入图片描述

 				
                View inflate = LayoutInflater.from(this).inflate(R.layout.myself,null);

                View img = inflate.findViewById(R.id.img);

                AlertDialog.Builder bd4 = new AlertDialog.Builder(this);

                bd4.setView(inflate);

                final AlertDialog dg4 = bd4.create();

                img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dg4.dismiss();
                    }
                });

                dg4.show();
5. 水平进度条对话框

在这里插入图片描述

 				
               final ProgressDialog progressDialog = new ProgressDialog(this);

                progressDialog.setTitle("这是水平进度条");

                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                progressDialog.setMax(100);


                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {

                    int progress = 0;

                    @Override
                    public void run() {
                        if (progress >= 100){
                            timer.cancel();
                            progressDialog.dismiss();
                        }else {
                            progress += 20;
                        }

                        progressDialog.setProgress(progress);
                    }
                },1000,1000);

                progressDialog.show();
6. 模糊进度条对话框

在这里插入图片描述

 			final ProgressDialog progressDialog1 = new ProgressDialog(this);

                progressDialog1.setTitle("这是水平进度条");

                progressDialog1.setProgressStyle(ProgressDialog.STYLE_SPINNER);

                progressDialog1.setMax(100);


                final Timer timer1 = new Timer();
                timer1.schedule(new TimerTask() {

                    int progress = 0;

                    @Override
                    public void run() {
                        if (progress >= 100){
                            timer1.cancel();
                            progressDialog1.dismiss();
                        }else {
                            progress += 20;
                        }

                        progressDialog1.setProgress(progress);
                    }
                },1000,1000);

                progressDialog1.show();
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_LONG).show();
                    }
                },instance.get(Calendar.YEAR),instance.get(Calendar.MONDAY),instance.get(Calendar.DAY_OF_MONTH)).show();
8. 时间进度条对话框

在这里插入图片描述

 			
                Calendar calendar = 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_LONG).show();
                    }
                },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();

二. 自定义对话框

第一步:新建一个类,继承Dialog	(接口回调)
package com.example.happy;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyDailog extends Dialog {

    private TextView title;
    private TextView xi;
    private Button no;
    private Button yes;

    private String titleStr;
    private String xiStr;

    public MyDailog( Context context) {
        super(context);
    }

    // 定义一个接口
    public interface noClickListener{
        void noClick();
    }

    // 创建一个接口对象
    private noClickListener listener;
    private noClickListener clickListener;

    public void setListener(noClickListener listener) {
        this.listener = listener;
    }

    public void setClickListener(noClickListener clickListener) {
        this.clickListener = clickListener;
    }

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

        title = (TextView) findViewById(R.id.title);
        xi = (TextView) findViewById(R.id.xi);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);

        if (titleStr != null){
            title.setText(titleStr);
        }

        if (xiStr != null){
            xi.setText(xiStr);
        }
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.noClick();
            }
        });

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.noClick();
            }
        });
    }

    public void setTitleStr(String titleStr) {
        this.titleStr = titleStr;
    }

    public void setXiStr(String xiStr) {
        this.xiStr = xiStr;
    }
}

第二步: 在主页面创建出自定义的Dialog类,实例化对象,调用方法

				final MyDailog myDailog = new MyDailog(this);

                myDailog.setTitleStr("笑一个吧");
                myDailog.setXiStr("我是李晓洁的男朋友");

                // 初始化接口数据
                myDailog.setListener(new MyDailog.noClickListener() {
                    @Override
                    public void noClick() {
                        myDailog.dismiss();
                    }
                });

                // 初始化接口数据
                myDailog.setClickListener(new MyDailog.noClickListener() {
                    @Override
                    public void noClick() {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                        myDailog.dismiss();
                    }
                });

                myDailog.show();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值