Dialog的详细使用

1、自己虽然一直使用过dialog,但是一直都是复制、粘贴;不清楚dialog的具体用途,这次趁着有时间,总结一下具体用法。

当在自定义dialog时,其实原理时一样的,通过代码写一个view,然后将new的dialog添加上view即可。

也可以直接写一个类继承dialog。然后调用即可。

 

代码下载地址:https://download.csdn.net/download/f552126367/10445244

dialog可以直接new一个alertDialog,然后进行附上你要添加的属性,具体包括:

包括普通Dialog
列表Dialog
单选Dialog
多选Dialog
等待Dialog
进度条Dialog
编辑Dialog
自定义Dialog

代码如下:

 

public class MainDialogActivity extends BaseActivity{
    private int yourChoice;
    private ArrayList<Integer> yourChoices = new ArrayList<>();
    private ProgressWheel pw;
    private Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_dialog);
        ButterKnife.bind(this);
    }
    /* @setIcon 设置对话框图标
     * @setTitle 设置对话框标题
     * @setMessage 设置对话框消息提示
     * setXXX方法返回Dialog对象,因此可以链式设置属性
     * */
    @OnClick(R.id.dialog_1)
    public void Dialog1(){
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainDialogActivity.this);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("我是一个普通Dialog");
        normalDialog.setMessage("你要点击哪一个按钮呢?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort("点击了确定");
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort("点击了关闭");
                    }
                });
        // 显示
        normalDialog.show();
    }

    /*列表Dialog*/
    @OnClick(R.id.dialog_2)
    public void Dialog2(){
        final String[] items = { "我是1","我是2","我是3","我是4" };
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        listDialog.setTitle("我是一个列表Dialog");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showToastShort("你点击了" + items[which]);
            }
        });
        listDialog.show();
    }
    /*单选Dialog*/
    @OnClick(R.id.dialog_3)
    public void Dialog3(){
        final String[] items = { "我是1","我是2","我是3","我是4" };
        yourChoice = -1;
        AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        singleChoiceDialog.setTitle("我是一个单选Dialog");
        // 第二个参数是默认选项,此处设置为0
        singleChoiceDialog.setSingleChoiceItems(items, 0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoice = which;
                    }
                });
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoice != -1) {
                            showToastShort("你选择了" + items[yourChoice]);
                        }
                    }
                });
        singleChoiceDialog.show();
    }
    /*多选Dialog*/
    @OnClick(R.id.dialog_4)
    public void Dialog4(){
        final String[] items = { "我是1","我是2","我是3","我是4" };
        // 设置默认选中的选项,全为false默认均未选中
        final boolean initChoiceSets[]={false,false,false,false};
        yourChoices.clear();
        AlertDialog.Builder multiChoiceDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        multiChoiceDialog.setTitle("我是一个多选Dialog");
        multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        if (isChecked) {
                            yourChoices.add(which);
                        } else {
                            yourChoices.remove(which);
                        }
                    }
                });
        multiChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int size = yourChoices.size();
                        String str = "";
                        for (int i = 0; i < size; i++) {
                            str += items[yourChoices.get(i)] + " ";
                        }
                        showToastShort("你选中了" + str);
                    }
                });
        multiChoiceDialog.show();
    }

    /*等待Dialog*/
    @OnClick(R.id.dialog_5)
    public void Dialog5(){
        ProgressDialog waitingDialog= new ProgressDialog(MainDialogActivity.this);
        waitingDialog.setTitle("我是一个等待Dialog");
        waitingDialog.setMessage("等待中...");
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(false);
        waitingDialog.show();
    }

    /*进度条dailog
    @setProgress 设置初始进度
    * @setProgressStyle 设置样式(水平进度条)
    * @setMax 设置进度最大值
    */
    @OnClick(R.id.dialog_6)
    public void Dialog6(){
        final int MAX_PROGRESS = 100;
        final ProgressDialog progressDialog =
                new ProgressDialog(MainDialogActivity.this);
        progressDialog.setProgress(0);
        progressDialog.setTitle("我是一个进度条Dialog");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(MAX_PROGRESS);
        progressDialog.show();
    /* 模拟进度增加的过程,新开一个线程,每个100ms,进度增加1*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress= 0;
                while (progress < MAX_PROGRESS){
                    try {
                        Thread.sleep(100);
                        progress++;
                        progressDialog.setProgress(progress);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                // 进度达到最大值后,窗口消失
                progressDialog.cancel();
            }
        }).start();
    }
    /*编辑dialog*/
    @OnClick(R.id.dialog_7)
    public void Dialog7(){
        final EditText editText = new EditText(MainDialogActivity.this);
        AlertDialog.Builder inputDialog =new AlertDialog.Builder(MainDialogActivity.this);
        inputDialog.setTitle("我是一个输入Dialog").setView(editText);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort(editText.getText().toString());
                    }
                }).show();
    }
    /*自定义dialog*/
    @OnClick(R.id.dialog_8)
    public void Dialog8(){
        
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.progress_dialog, null);
dialog = new Dialog(this, R.style.confirm_dialog);
dialog.setContentView(layout);
dialog.show();
pw = layout.findViewById(R.id.progressBar_dialog);
pw.resetCount();
pw.setText("JD");
pw.spin();
dialog.setCanceledOnTouchOutside(false);

}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页