美文网首页
Android中常用的七种对话框AlertDialog

Android中常用的七种对话框AlertDialog

作者: 大道至简_知易行难 | 来源:发表于2019-01-22 14:21 被阅读0次

    平时只是使用没来得及总结,现在把7种常用的对话框手敲一边

    1、普通对话框

     private void showNormalDialog() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("是否退出");
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setCancelable(false);
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    

    2、等待对话框

     private void showWaitingDialog() {
            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("提示");
            progressDialog.setMessage("进行中...");
            progressDialog.setIndeterminate(true);//模糊进度
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
    

    3、进度对话框

    private void showProgressDialog() {
            final int MAX_PROGRESS = 100;
            final ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setProgress(0);
            progressDialog.setTitle("进度条");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMax(MAX_PROGRESS);
            progressDialog.show();
            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();
    
        }
    

    4、列表选择对话框

     private void showListDialog() {
            final String items[] = {"AA", "BB", "CC"};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("列表对话框");
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setCancelable(false);
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(AlertDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
                }
            });
            builder.create().show();
        }
    

    5、单选对话框

      private void showSingleChoiceDialog() {
            final String items[] = {"AA", "BB", "CC"};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("单选");
            builder.setCancelable(false);
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(AlertDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
                }
            });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    

    6、多选对话框

     private void showMulChoiceDialog() {
            final String items[] = {"篮球", "足球", "排球"};
            final boolean selected[] = {true, false, true};
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("多选");
            builder.setCancelable(false);
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    Toast.makeText(AlertDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
                }
            });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    

    7、自定义对话框

    private void showMyDialog() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("我的对话框");
            builder.setCancelable(false);
            builder.setIcon(R.mipmap.ic_launcher);
            View view = getLayoutInflater().inflate(R.layout.view_my_dialog, null);
            builder.setView(view);
            EditText et_name = view.findViewById(R.id.et_name);
            final String name = et_name.getText().toString().trim();
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(AlertDialogActivity.this, name, Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    

    相关文章

      网友评论

          本文标题:Android中常用的七种对话框AlertDialog

          本文链接:https://www.haomeiwen.com/subject/knvljqtx.html