自定义警报对话框不会被关闭

custom alert dialog is not getting dismissed

我创建了一个自定义对话框,我在此处使用自定义布局扩充了一个对话框视图,该对话框已成功显示,但是当我尝试关闭该对话框时,没有任何反应,我尝试将该对话框声明为最终对话框,但它没有'帮助。

请看我的代码:

final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
                            LayoutInflater inflater = getActivity().getLayoutInflater();
                            final View dialogView = inflater.inflate(R.layout.fragment_itemdet, null);
                            dialogBuilder.setView(dialogView);

                            name=dialogView.findViewById(R.id.itemname);
                            img=dialogView.findViewById(R.id.itemdetimg);
                            counter=dialogView.findViewById(R.id.counter);
                            add=dialogView.findViewById(R.id.add);
                            del=dialogView.findViewById(R.id.sub);
                            confirm=dialogView.findViewById(R.id.confirm);
                            Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(),  "fonts/Poppins-ExtraBoldItalic.ttf");
                            name.setTypeface(custom_font);
                            final AlertDialog alertDialog = dialogBuilder.create();
                            add.setOnClickListener(v -> counter.setText(String.valueOf(i++)));
                            del.setOnClickListener(v -> counter.setText(String.valueOf(i--)));
                            name.setText(docname);
                            Glide.with(getContext()).load(url).into(img);
                            confirm.setOnClickListener(v->{
                                String qty=counter.getText().toString();
                                if (qty.equals("0")){
                                    Toast.makeText(getContext(),"Please Specify Quantity",Toast.LENGTH_LONG).show();
                                }
                                else {
                                    db=FirebaseFirestore.getInstance();
                                    db.collection("Order").document(TableListFragment.tableno)
                                            .update(
                                                    "Items", FieldValue.arrayUnion(docname),
                                                    "Quantity", FieldValue.arrayUnion(qty)
                                            ).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                      Toast.makeText(getContext(),"Item Added",Toast.LENGTH_LONG).show();
                                            alertDialog.dismiss();
                                        }
                                    }).addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.i("WhatdFuck:",e.toString());
                                        }
                                    });

                                }
                            });
                            dialogBuilder.setTitle("Order");
                            dialogBuilder.setMessage("Enter Order Details");
                          final AlertDialog b = dialogBuilder.create();
                            b.show();
                        }
                    });

将您的 alertDialog.dismiss(); 移动到 if (qty.equals("0")){ 的上方,如下所示:

alertDialog.dismiss();
if (qty.equals("0")){...

更新

出现问题是因为您正在显示不同的对话框 (b) 并关闭不同的对话框 (alertDialog):

balertDialog 都是不同的对话框,所以替换这两行:

final AlertDialog b = dialogBuilder.create();
                            b.show();

只有这个:

alertDialog.show();

在代码底部。

您也可以使用对话框代替下面的 AlertDialog.Builder。您也可以自定义它;

     final Dialog dialog = new Dialog(MyFirebaseMessagingService.this);
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
     dialog.setContentView(R.layout.alert_my_tasks);
     dialog.setCancelable(true);

     TextView tv_msg = dialog.findViewById(R.id.tv_msg);
     TextView tv_reachedLoc = dialog.findViewById(R.id.tv_reachedLoc);

     tv_msg .setText("");

     tv_reachedLoc.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                   // dismiss dialog
                                    dialog.dismiss();

                                   // do your task here

                                }
                            });

  dialog.show();
  }

试试这个代码

final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.fragment_itemdet);

        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.show();
                            name=dialog.findViewById(R.id.itemname);
                            img=dialog.findViewById(R.id.itemdetimg);
                            counter=dialog.findViewById(R.id.counter);
                            add=dialog.findViewById(R.id.add);
                            del=dialog.findViewById(R.id.sub);
                            confirm=dialog.findViewById(R.id.confirm);
                            Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(),  "fonts/Poppins-ExtraBoldItalic.ttf");
                            name.setTypeface(custom_font);
 add.setOnClickListener(v -> counter.setText(String.valueOf(i++)));
                            del.setOnClickListener(v -> counter.setText(String.valueOf(i--)));
                            name.setText(docname);
                            Glide.with(getContext()).load(url).into(img);
                            confirm.setOnClickListener(v->{
                              dialog.dismiss();
                                String qty=counter.getText().toString();
                                if (qty.equals("0")){
                                    Toast.makeText(getContext(),"Please Specify Quantity",Toast.LENGTH_LONG).show();
                                }
                                else {
                                    db=FirebaseFirestore.getInstance();
                                    db.collection("Order").document(TableListFragment.tableno)
                                            .update(
                                                    "Items", FieldValue.arrayUnion(docname),
                                                    "Quantity", FieldValue.arrayUnion(qty)
                                            ).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                      Toast.makeText(getContext(),"Item Added",Toast.LENGTH_LONG).show();
                                            alertDialog.dismiss();
                                        }
                                    }).addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            Log.i("WhatdFuck:",e.toString());
                                        }
                                    });

                                }
                            });

                        }
                    });

您在两个不同的地方调用 dialogBuilder.create 的 problem.is 并且永远不会显示 alertDialog 实例。

删除行:

...b = dialogBuilder.create();
b.show();

并在同一个地方添加:

alertDialog.show();

此外,您还必须修复所有内容的顺序以使其正常工作。

  • 实例化构建器
  • 向构建器添加参数
  • 通过调用 dialogBuilder.create()
  • 创建对话框
  • 在拥有对话实例后创建点击侦听器
  • 显示对话框

尝试关闭 UI 线程中的对话框

runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    alertDialog.dismiss();
                }
            });

只需使用

 alertDialog.show();

而不是这两行

final AlertDialog b = dialogBuilder.create();
                        b.show();

替换最后两行代码。当您创建两个对话框实例并试图关闭您未显示的对话框时,它会产生问题。

        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.fragment_itemdet, null);
        dialogBuilder.setView(dialogView);

        name = dialogView.findViewById(R.id.itemname);
        img = dialogView.findViewById(R.id.itemdetimg);
        counter = dialogView.findViewById(R.id.counter);
        add = dialogView.findViewById(R.id.add);
        del = dialogView.findViewById(R.id.sub);
        confirm = dialogView.findViewById(R.id.confirm);
        Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Poppins-ExtraBoldItalic.ttf");
        name.setTypeface(custom_font);
        final AlertDialog alertDialog = dialogBuilder.create();
        add.setOnClickListener(v -> counter.setText(String.valueOf(i++)));
        del.setOnClickListener(v -> counter.setText(String.valueOf(i--)));
        name.setText(docname);
        Glide.with(getContext()).load(url).into(img);
        confirm.setOnClickListener(v -> {
            String qty = counter.getText().toString();
            if (qty.equals("0")) {
                Toast.makeText(getContext(), "Please Specify Quantity", Toast.LENGTH_LONG).show();
            } else {
                db = FirebaseFirestore.getInstance();
                db.collection("Order").document(TableListFragment.tableno)
                        .update(
                                "Items", FieldValue.arrayUnion(docname),
                                "Quantity", FieldValue.arrayUnion(qty)
                        ).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast.makeText(getContext(), "Item Added", Toast.LENGTH_LONG).show();
                        alertDialog.dismiss();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.i("WhatdFuck:", e.toString());
                    }
                });

            }
        });
        dialogBuilder.setTitle("Order");
        dialogBuilder.setMessage("Enter Order Details");


         alertDialog.show();