Cannot close a custom AlertDialog

戏子无情 提交于 2020-12-15 06:25:06

问题


so I have an app with three different buttons(parent buttons) on pressing any of them, the custom alert dialog with 9 different buttons is displayed but the functions of these 9 buttons within the alert dialog are different depending on which of the three parent buttons called it. On pressing any of the 9 buttons, I want the app to perform a particular function and then close the alertdialog. Now the problem is that I can easily call the alert dialog by calling the method showcustomdialog(); that I created but I can't dismiss it using alertdialog.dismiss(); inside the OnClickListener of the parent button since the method has a void result type. I've tried using if-else statements but it doesn't work. How can I achieve what is required?

The method:

    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();
      }

I am calling the meathod and using it as follows:

    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.
            }
        });

and so on.


回答1:


I figured out a solution for you using AlertDialog.

  parentbutton1.setOnClickListener(new View.OnClickListener() {

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

        });

Then the method MUST be a private or public 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;
    
}

P.S. Replace the ids I used to create this sample with yours.



来源:https://stackoverflow.com/questions/64630074/cannot-close-a-custom-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!