手记

Android中的AlertDialog

一.普通对话框
演示图片:

demo代码:

    class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
               case R.id.btn_dialog1:
                   AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                    builder.setTitle("请回答")
                           .setMessage("你觉得Android是一种什么样的语言")
                            .setIcon(R.mipmap.username)
                           .setPositiveButton("神奇的语言", new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialogInterface, int which) {
                                   ToastUtil.showMsg(DialogActivity.this,"很好呀");
                               }
                           }).setNeutralButton("还行", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,"在思考一下~");
                        }
                    }).setNegativeButton("不神奇", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,"怎么可能");
                        }
                    }).show();
                    break;
            }
        }
    }

二.单选对话框
运行图片

demo代码:

                case R.id.btn_dialog2:
                    final String[] array = new String[]{"男","女"};
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                    builder1.setTitle("选择性别")
                            .setItems(array, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ToastUtil.showMsg(DialogActivity.this,array[i]);

                                }
                            }).show();

复选按钮形式展示:

demo代码展示:

            case R.id.btn_dialog3:
                    final String[] array3 = new String[]{"男","女"};
                    AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
                    builder3.setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ToastUtil.showMsg(DialogActivity.this,array3[i]);

                        }
                    }).show();
                    break;

三.多选弹窗:

demo代码:

       case R.id.btn_dialog4:
                    final String[] array4 = new String[]{"唱歌","跳舞","写代码"};
                    final boolean[] isSelected = new boolean[]{false,false,true};
                    AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
                    builder4.setTitle("选择兴趣")
                            .setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i, boolean isChecked) {
                                    ToastUtil.showMsg(DialogActivity.this,array4[i]+":"+isChecked);
                                }
                            }).show();

四.自定义dialog

demo代码:

                case R.id.btn_dialog5:
                    AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
                    View view= LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
                    EditText etUsername = view.findViewById(R.id.et_username);
                    EditText etPassword = view.findViewById(R.id.et_password);
                    Button btnLogin = view.findViewById(R.id.btn_login);
                    btnLogin.setOnClickListener(new View.OnClickListener() {
                            @Override
                        public void onClick(View view) {
                            //相应操作
                        }
                    });
                    builder5.setTitle("请先登录")
                            .setView(view).show();
                    break;
1人推荐
随时随地看视频
慕课网APP