自定义的dialog

package com.example.zhuangaoyipplication;

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

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

    private String noStr;
    private String titleStr;
    private String messageStr;

    public MyselfCustom(@NonNull Context context) {
        super(context);
    }

    public interface NoClickListener{
        void click();
    }
    public interface YesClickListener{
        void click();
    }
    private YesClickListener yesClickListener;
    private NoClickListener noClickListener;

    public void setYesClickListener(YesClickListener yesClickListener) {
        this.yesClickListener = yesClickListener;
    }

    public void setNoClickListener(String str,NoClickListener noClickListener) {
        if (str != null){
            noStr=str;
        }
        this.noClickListener = noClickListener;
    }

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

        title = (TextView) findViewById(R.id.title);
        message = (TextView) findViewById(R.id.message);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);
        if (noStr!=null){
            no.setText(noStr);
        }
        if (titleStr!=null){
            title.setText(titleStr);
        }
        if (messageStr!=null){
            message.setText(messageStr);
        }
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noClickListener.click();
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yesClickListener.click();
            }
        });
    }

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

    public void setMessageStr(String messageStr) {
        this.messageStr = messageStr;
    }
}
```java
package com.example.zhuangaoyipplication;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainBctivity extends AppCompatActivity {
    private Button norma;
    private Button single;
    private Button checks;
    private Button hol;
    private Button round;
    private Button custom;
    private Button date;
    private Button timeDate;
    private Button unual;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_bctivity);
        timeDate = (Button) findViewById(R.id.time_date);
        single = (Button) findViewById(R.id.single);
        norma = (Button) findViewById(R.id.norma);
        checks = (Button) findViewById(R.id.checks);
        hol = (Button) findViewById(R.id.hol);
        round = (Button) findViewById(R.id.round);
        custom = (Button) findViewById(R.id.custom);
        date = (Button) findViewById(R.id.date);
        unual = (Button) findViewById(R.id.unual);

        unual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyselfCustom myselfCustom = new MyselfCustom(MainBctivity.this);
                myselfCustom.setTitleStr("我也不知道");
                myselfCustom.setMessageStr("少看手机");
                myselfCustom.setNoClickListener("火辣化垃圾里",new MyselfCustom.NoClickListener() {
                    @Override
                    public void click() {
                        Toast.makeText(MainBctivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                myselfCustom.setYesClickListener(new MyselfCustom.YesClickListener() {
                    @Override
                    public void click() {
                        Toast.makeText(MainBctivity.this, "确定了我点击的", Toast.LENGTH_SHORT).show();
                    }
                });
                myselfCustom.show();
            }
        });
        //时间对话框
        timeDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Timedate();
            }
        });
        //日期对话框
        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dat();
            }
        });
        //自定义对话框
        custom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Custom();
            }
        });
        //多选对话框
        checks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                che();
            }
        });
        //圆形进度条对话框
        round.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roun();
            }
        });
        //水平进度条对话框
        hol.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hol();
            }
        });
        //普通对话框
        norma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                norma();
            }
        });
        // 单选对话框
        single.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                single();
            }
        });
    }
    //时间对话框
    public void Timedate(){
        Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(MainBctivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainBctivity.this, hourOfDay+"-"+minute, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
    }
    //日期对话框
    public void Dat(){
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(MainBctivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainBctivity.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 Custom(){
        View inflate = LayoutInflater.from(MainBctivity.this).inflate(R.layout.customlayout, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainBctivity.this);
        ImageView viewById = inflate.findViewById(R.id.imv);
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "ok", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "cancel", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setView(inflate);
        final AlertDialog alertDialog = builder.create();
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
    }
    //圆形
    public void roun(){
        final ProgressDialog dialog = new ProgressDialog(MainBctivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setTitle("这是标题");
        dialog.setIcon(R.mipmap.ic_launcher);
        dialog.show();
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;
            @Override
            public void run() {
                if (progress==100){
                    dialog.dismiss();
                    timer.cancel();
                }
                dialog.setProgress(progress+=20);
            }
        },0,1000);

    }
    //多选对话框
    public void che(){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainBctivity.this);
        final String[] strs = {"张三","李四","王二麻子"};
        boolean[] bols = {true,false,true};
        builder.setMultiChoiceItems(strs, bols, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked){
                    Toast.makeText(MainBctivity.this, strs[which], Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "ok", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "cancel", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    public void hol(){
        final ProgressDialog dialog = new ProgressDialog(MainBctivity.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.setMessage("正在下载");
        dialog.show();
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;
            @Override
            public void run() {
                if (progress==100){
                    dialog.dismiss();
                    timer.cancel();
                }
                dialog.setProgress(progress+=20);
            }
        },0,1000);
    }
    //这是单选对话框
    public void single(){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainBctivity.this);
        final String[] strs = {"123","456","789"};
        builder.setSingleChoiceItems(strs, 2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, strs[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "ok", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "cancel", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    //这是普通对话框
    public void norma(){
        AlertDialog.Builder builder = new AlertDialog.Builder(MainBctivity.this);
        builder.setIcon(R.drawable.ic_launcher_background);
        builder.setTitle("这是标题");
        builder.setMessage("这是内容");
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainBctivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值