How to dismis custom dialog from a custom button in dialog?

我怕爱的太早我们不能终老 提交于 2020-01-06 12:41:13

问题


Hi there,

    @Override
    protected Dialog onCreateDialog(int id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        inviteView = getLayoutInflater().inflate(R.layout.invite_dialog, null);
        builder.setView(inviteView);
        sendSmsButton = (Button) inviteView.findViewById(R.id.sendSMSButton);
        sendEmailButton = (Button) inviteView.findViewById(R.id.sendEmailButton);


        builder.setTitle(R.string.invite_callrz_title);
        sendSmsButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        sendEmailButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        builder.setNegativeButton(R.string.cancelItem,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        return builder.create();
    }

I have a custom layout with 2 buttons on it.I also use a default nagativeButton to dismis the dialog. My question is I would like to dismis the dialog bar when the custom buttons are clicked. Basically, when the button is clicked, it will do some staff and dismis the dialog. which method I should call?


回答1:


try this

AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog alertDialog; 
alertDialog = builder.create();

sendSmsButton = (Button) builder.findViewById(R.id.sendSMSButton);

sendSmsButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            alertDialog.dismiss();
        }
    });


来源:https://stackoverflow.com/questions/8091858/how-to-dismis-custom-dialog-from-a-custom-button-in-dialog

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