Android:AlertDialog未在Cancel()或Dismiss()上关闭

时间:2018-06-30 14:20:56

标签: android onclicklistener alertdialog android-alertdialog

我已经实现了AlertDialog,它可以用作消息对话框。用户键入消息时,他可以单击按钮“发送”或按钮“取消”。 “发送”按钮可以正常工作,但是“取消”按钮由于某些原因不起作用。

findViewById(R.id.contactfab).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Frontpage.this);
                final AlertDialog dialog = dialogBuilder.create();
                LayoutInflater inflater = Frontpage.this.getLayoutInflater();
                View dialogView = inflater.inflate(R.layout.dialog_contact, null);
                dialogBuilder.setView(dialogView);

                final EditText editText = (EditText) dialogView.findViewById(R.id.contactText);
                editText.setText("");
                dialogView.findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String [] reciever = new String[]{"test@hotmail.com"};
                        String subject = ("Feedback");
                        Intent mailIntent = new Intent(Intent.ACTION_SEND);
                        mailIntent.putExtra(Intent.EXTRA_EMAIL, reciever);
                        mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                        mailIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString());
                        mailIntent.setType("message/rfc822");
                        startActivity(Intent.createChooser(mailIntent, "Vælg en applikation til at sende din mail med"));
                    }
                });

                dialogView.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        dialog.cancel();
                    }
                });

                dialogBuilder.show();
            }
        });

为了测试它是否正在监听取消按钮,我试图使其在单击按钮时打印一个字符串。字符串被打印出来,但是对话框没有关闭。已尝试使用dialog.dismiss()dialog.dismiss()

我想念什么?

1 个答案:

答案 0 :(得分:2)

调用dialogBuilder.show();时,它将创建一个新的AlertDialog,其中包含提供给构建器的参数,并立即显示该对话框。结果,这个新的AlertDialog将不同于使用final AlertDialog dialog声明的那个。简而言之,声明的代码dialog将永远不会显示,因此dialog.cancel()dialog.close()不会有任何影响。

如下更改代码:

findViewById(R.id.contactfab).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view){
            final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Frontpage.this);

            LayoutInflater inflater = Frontpage.this.getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.dialog_contact, null);
            dialogBuilder.setView(dialogView);
            final EditText editText = (EditText) dialogView.findViewById(R.id.contactText);
            editText.setText("");
            final AlertDialog dialog = dialogBuilder.create();
            dialogView.findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String [] reciever = new String[]{"test@hotmail.com"};
                    String subject = ("Feedback");
                    Intent mailIntent = new Intent(Intent.ACTION_SEND);
                    mailIntent.putExtra(Intent.EXTRA_EMAIL, reciever);
                    mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
                    mailIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString());
                    mailIntent.setType("message/rfc822");
                    startActivity(Intent.createChooser(mailIntent, "Vælg en applikation til at sende din mail med"));
                }
            });

            dialogView.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view) {
                    dialog.cancel();
                }
            });

            dialog.show();
        }
    });
}