复选框未显示在android的弹出窗口内

问题描述 投票:0回答:1

我正在尝试添加一个带有复选框的弹出窗口,如果未选中该复选框,则它应该不能在弹出窗口上按Yes,并且更好的是在弹出窗口中出现某种错误消息。我的尝试如下。

    AlertDialog.Builder builder = new AlertDialog.Builder(EmptyActivity.this);
    builder.setTitle(R.string.app_name);
    builder.setMessage("Confirm for go?");
    final CharSequence[] items = {"Something to check before going "};
    final ArrayList selectedItems=new ArrayList();
    builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            //Here you add or remove the items from the list selectedItems. That list will be the result of the user selection.
            if(isChecked) {
                selectedItems.add(which);
            }
        }
    });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
            if(selectedItems.size() == 1) {
                //call going api
            }
            else{
                //show an error msg inside pop up to tick the checkbox and unable to click yes
            }
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

弹出如下图所示。

enter image description here

我在运行弹出窗口时看不到该复选框。所以有人知道我错了吗?我应该如何在弹出窗口中显示错误消息,以通知用户在单击“是”之前先勾选复选框?

java android android-alertdialog
1个回答
0
投票
    private void showDialogue() {

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
            LayoutInflater inflater = this.getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.custom_dashboard, null);
            dialogBuilder.setView(dialogView);
            final AlertDialog alertDialog = dialogBuilder.create();
            final CheckBox one = dialogView.findViewById(R.id.chk01);

            Button done = dialogView.findViewById(R.id.done);
            done.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (one.isChecked()) {
                  //do something
                }else{
 //do something               
}
            });
            alertDialog.show();


        }
© www.soinside.com 2019 - 2024. All rights reserved.