Closing a custom alert dialog on button click

前端 未结 10 1255
灰色年华
灰色年华 2020-12-06 04:01

I\'m having trouble closing my alert dialog. I am using a layout inflator to make the dialog, so I\'m not sure how I would go about closing the thing after I\'m done with it

10条回答
  •  时光说笑
    2020-12-06 04:58

    i have modified your code plz check..

       AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
        DialogInterface dia = new DialogInterface();
    
        //Create a custom layout for the dialog box
        LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);
    
        TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
        Button add_item = (Button)layout.findViewById(R.id.add_button);
    
    
    
        final AlertDialog Dial = alertDialog.create();
        title.setText(workout_items[position]);
        Dial.setView(layout);
    
        AlertDialog alertDialog = dialog.create();
    
        add_item.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Dial.dismiss();
            }
        });
    
    
        Dial.show();
    

    Just Added

    final AlertDialog Dial = alertDialog.create(); and change dialog.setView(layout); to Dial.setView(layout);

    now just call Dial.dismiss(); in onclick listener.. works fine for me.

提交回复
热议问题