张萌&韩墨羽——Dialog对话框-LMLPHP

一.常用的对话框

1.普通对话框

public void MyCustomDialog(){
        //TODO 1:构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //TODO 2:设置属性
        builder.setTitle("韩墨羽");
        builder.setMessage("好喜欢你");
        builder.setIcon(R.mipmap.y);

        //TODO 设置确定按钮
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了确认", Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });

        //TODO 3:使用建造者创建对话框
        AlertDialog dialog = builder.create();
        //TODO 4:显示
        dialog.show();
    }

2.单选对话框

public void MySingleDialoy(){
        //TODO 1:构建者
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //TODO 2:设置属性
        builder.setIcon(R.drawable.ic_launcher_background);//设置图标
        builder.setTitle("喜欢你的核心要素");//设置标题
        //TODO 设置确定按钮
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了确认", Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });

        final String[] itmes={"时间","空间","机遇"};
        //TODO 设置单选列表
        //TODO  参数一 列表  参数二 默认选中下表  参数三 事件监听
        builder.setSingleChoiceItems(itmes, 2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你选择的是"+itmes[which], Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 3:使用建造者创建对话框
        AlertDialog dialog = builder.create();
        //TODO 4:显示
        dialog.show();
    }

3.多选对话框

public void MyMuliteDialog(){
        final String[] items={"许梦欣","王思萌","杨迪","徐可冉","赵璐","贺庆","黄珂","杨雅菲","韩墨羽"};
        final boolean[] flag={true,false,false,false,false,false,false,false,false};
        //TODO 1:构建者
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //TODO 2:设置属性
        builder.setIcon(R.mipmap.y);
        builder.setTitle("在一起过的人");
        //TODO 设置确定按钮
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                for (int i = 0; i < flag.length; i++) {
                    if (flag[i]){
                        Toast.makeText(MainActivity.this, "是"+items[i], Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        //TODO 设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 设置多选列表
        //TODO  参数一 列表  参数二 默认选中下表  参数三 事件监听
        builder.setMultiChoiceItems(items, flag, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                flag[which]=isChecked;
            }
        });
        //TODO 3:使用建造者创建对话框
        AlertDialog alertDialog = builder.create();
        //TODO 4:显示
        alertDialog.show();
    }

4.自定义对话框

public void MyDialog(){
        //TODO 1:构建者
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        //TODO 2:设置属性
        builder.setTitle("老婆");
        builder.setIcon(R.mipmap.y);
        //TODO 3:设置内部视图
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.u);
        builder.setView(imageView);
        //TODO 设置确定按钮
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了确认", Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 设置取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 3:使用建造者创建对话框
        AlertDialog alertDialog = builder.create();
        //TODO 4:显示
        alertDialog.show();
    }

5.水平进度条对话框

private void MyHorizontalDialog() {
        //TODO 1:构建者
        final ProgressDialog dialog = new ProgressDialog(this);
        //TODO 2:设置属性
        dialog.setIcon(R.mipmap.y);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setTitle("爱的加载");
        dialog.setMax(100);
        dialog.setMessage("慢慢的爱不停...");
        //TODO 设置确定按钮
        dialog.setButton(DialogInterface.BUTTON_POSITIVE,"确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了确认", Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 设置取消按钮
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress=0;
            @Override
            public void run() {
                if (progress==dialog.getMax()){
                    timer.cancel();
                }
                dialog.setProgress(progress+=1);
            }
        },0,10);
    }

6.圆形进度条对话框

private void MySpinnerDialog() {
        //TODO 1:构建者
        final ProgressDialog dialog = new ProgressDialog(this);
        //TODO 2:设置属性
        dialog.setIcon(R.mipmap.y);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setTitle("爱的加载");
        dialog.setMax(100);
        dialog.setMessage("慢慢的爱不停...");
        //TODO 设置确定按钮
        dialog.setButton(DialogInterface.BUTTON_POSITIVE,"确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了确认", Toast.LENGTH_SHORT).show();
            }
        });
        //TODO 设置取消按钮
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
    }

7.日期选择对话框

private void MyDateDialog() {
        Calendar calendar = Calendar.getInstance();
        /**
         * @param context 上下文
         * @param listener 监听器
         */
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            /**
             *
             * @param view
             * @param year 默认的年
             * @param month 默认的月
             * @param dayOfMonth 默认的日
             */
            @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();
    }

8.时间选择对话框

private void MyTimeDialog() {
        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_SHORT).show();
            }
        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
    }

二.自定义对话框

布局

private void ziDingYi_Dialog() {
        MyDialog_diy myDialog_diy = new MyDialog_diy(this);
        myDialog_diy.setTitle("韩墨羽");
        myDialog_diy.setMessage("真的好喜欢你");
        myDialog_diy.setYesOnclickListener("确认",new MyDialog_diy.onYesOnclickListener(){
            @Override
            public void onYesOnclick() {
                Toast.makeText(MainActivity.this, "确认", Toast.LENGTH_SHORT).show();

            }
        });
        myDialog_diy.setNoOnclickListener("取消", new MyDialog_diy.onNoOnclickListener() {
            @Override
            public void onNoclick() {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog_diy.show();
    }

张萌&韩墨羽——Dialog对话框-LMLPHP

自定义类

public class MyDialog_diy extends Dialog {
    private Button yes;//确定按钮
    private Button no;//取消按钮
    private TextView titleTV;//消息标题文本
    private TextView message;//消息提示文本
    private String titleStr;//从外界设置的title文本
    private String messageStr;//从外界设置的消息文本
    private String yesStr, noStr;//确定文本和取消文本的显示的内容
    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    public MyDialog_diy(Context context, int themeResId) {
        super(context, themeResId);

        WindowManager windowManager = getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width=display.getWidth()*4/5;
        getWindow().setAttributes(layoutParams);
    }

    public MyDialog_diy(Context context) {
        super(context);
        //去掉默认的标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onclickListener
     */
    public void setNoOnclickListener(String str,onNoOnclickListener onclickListener){
        if (str!=null){
            noStr=str;
        }
        this.noOnclickListener=onclickListener;
    }
    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onclickListener
     */
    public void setYesOnclickListener(String str,onYesOnclickListener onclickListener){
        if (str!=null){
            yesStr=str;
        }
        this.yesOnclickListener=onclickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zidingyi_dialog);
        //空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        iniytData();
        //初始化界面控件的事件
        initEvent();
    }
    private void initView(){
        yes=findViewById(R.id.yes);
        no=findViewById(R.id.no);
        titleTV = findViewById(R.id.title);
        message = findViewById(R.id.message);
    }
    /**
     * 初始化界面控件的显示数据
     */
    private void iniytData(){
        if (titleStr!=null){
            titleTV.setText(titleStr);
        }
        if (messageStr!=null){
            message.setText(messageStr);
        }
        if (yesStr!=null){
            yes.setText(yesStr);
        }
        if (noStr!=null){
            no.setText(noStr);
        }
    }
    /**
     * 初始化界面的确定和取消监听
     */
    private void initEvent(){
        //设置确定按钮被点击后,向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener!=null){
                    yesOnclickListener.onYesOnclick();
                }
            }
        });
        //设置取消按钮被点击后,向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener!=null){
                    noOnclickListener.onNoclick();
                }
            }
        });
    }
    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title){
        titleStr=title;
    }
    public void setMessage(String message){
        messageStr=message;
    }
    public interface onNoOnclickListener{
        public void onNoclick();
    }
    public interface onYesOnclickListener{
        public void onYesOnclick();
    }
}
07-07 02:08