Android studio 无法关闭自定义警报对话框

Android studio 无法关闭自定义警报对话框,android-studio,Android Studio,因此,我有一个应用程序,它有三个不同的按钮(父按钮),当按下其中任何一个按钮时,会显示带有9个不同按钮的自定义警报对话框,但警报对话框中这9个按钮的功能会有所不同,这取决于三个父按钮中的哪一个调用了它。按下9个按钮中的任何一个,我希望应用程序执行特定功能,然后关闭alertdialog。现在的问题是,我可以通过调用方法showcustomdialog()轻松调用警报对话框是我创建的,但我不能使用alertdialog.disease()来取消它。我尝试过使用if-else语句,但不起作用。我怎样

因此,我有一个应用程序,它有三个不同的按钮(父按钮),当按下其中任何一个按钮时,会显示带有9个不同按钮的自定义警报对话框,但警报对话框中这9个按钮的功能会有所不同,这取决于三个父按钮中的哪一个调用了它。按下9个按钮中的任何一个,我希望应用程序执行特定功能,然后关闭alertdialog。现在的问题是,我可以通过调用方法
showcustomdialog()轻松调用警报对话框是我创建的,但我不能使用
alertdialog.disease()来取消它。我尝试过使用if-else语句,但不起作用。我怎样才能达到要求

方法:

    private void showCustomDialog() {
    ViewGroup viewGroup = findViewById(android.R.id.content);
    View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_main2, viewGroup, false);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialogView);
    final AlertDialog alertDialog = builder.create();
    alertDialog.show();
      }
我给meathod打电话,并按如下方式使用它:

    parentbutton1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        showCustomDialog();
        alertbutton1.getId();
        alertbutton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView1.setText("500");
                function();
             //I want to dismiss the alertdialog here.
            }
        });
        alertbutton2.getId();
        alertbutton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView1.setText("1000");
                function();
             //I want to dismiss the alertdialog here.
            }
        });

等等。

我使用AlertDialog为您找到了一个解决方案

  parentbutton1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                createDialog();
            }

        });
然后,该方法必须是私有或公共AlertDialog:

   private AlertDialog createDialog() {

    LayoutInflater inflater = (getActivity()).getLayoutInflater(); // for fragment or getLayoutInflater(); for activity
    View v = inflater.inflate(R.layout.dialog_add_new_list, null);
    Button okButton = v.findViewById(R.id.confirm_button);
    Button cancelButton = v.findViewById(R.id.cancel_button);
    
    final AlertDialog dialog = new AlertDialog.Builder(getActivity()) // for fragment or AlertDialog.Builder ( this ) for activity
        .setView(v)

        .show();
        
    okButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                
        }
    });
    cancelButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View p1) {
                dialog.dismiss();
            }


        });
    
    return dialog;
    
}

另外,请用您的ID替换我用来创建此示例的ID。

您是否尝试调用dismise()?它不起作用,因为正如我提到的,showcustomdialog()方法有一个无效的结果类型,我想在showcustomdialog()方法之外调用dismise函数。您从哪里获得的
alertbutton1
?它是对话框视图的一部分吗?是的,它是对话框视图的一部分。我本可以在
showcustomdialog()
方法中为按钮设置OnClickListeners,但正如我所提到的,根据调用alertdialog的父按钮,它们的功能是不同的,因此这样做不是一个选项。但alertButton1不是添加到alertdialog的视图的一部分。您是如何初始化alertButton1的?