从对话框上的按钮不同的电子邮件意图

时间:2013-03-27 07:53:25

标签: android email android-intent dialog

我只有一个带按钮的屏幕(我稍后会添加更多)。但是现在,我希望这样做,以便在单击按钮时,它将弹出一个包含两个选项的对话框。这两个选项都是电子邮件意图,它只是传递给电子邮件客户端的数据不同。这可能吗?我是一名新开发人员,这是我的初级项目之一,所以请耐心等待。提前致谢。

好的,所以我找到了答案(我把它放在onClick()方法中):

AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("Save File...");

            // Setting Dialog Message
            alertDialog.setMessage("Do you want to save this file?");

            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.save);

            // Person presses first option (first email)
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                // User pressed YES button. Write Logic Here
               Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "email@domain.com" });
                    emailIntent.setType("message/rfc822");
                    startActivity(Intent.createChooser(emailIntent, "Send email..."));
                }
            });

            // Person presses second option (second email)
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "example@domain.com" });
                    emailIntent.setType("message/rfc822");
                    startActivity(Intent.createChooser(emailIntent, "Send email..."));
                }
            });

            // Put a "cancel" button
            alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                // User pressed Cancel button. Write Logic Here
                Toast.makeText(getApplicationContext(), "You clicked on Cancel",
                                    Toast.LENGTH_SHORT).show();
                }
            });

            // Show the dialog
            alertDialog.show();

2 个答案:

答案 0 :(得分:1)

CharSequence[] arrayMail = {"first mail option", "second one"};
            builder.setTitle("Mail").setItems(arrayMail, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(which==0)
                    {
                        Intent emailIntent = new Intent(Intent.ACTION_SEND);
                        emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "first data" });
                        emailIntent.setType("text/plain");
                        startActivity(Intent.createChooser(emailIntent, "Send email ..."));
                    }
                    if(which==1)
                    {
                        Intent emailIntent = new Intent(Intent.ACTION_SEND);
                        emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "Second data" });
                        emailIntent.setType("text/plain");
                        startActivity(Intent.createChooser(emailIntent,"Send email"));
                    }
                }
            });
            builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            AlertDialog dialogSeguin = builder.create();
            dialogSeguin.show();

当然最好将“strings.xml”用于字符链

答案 1 :(得分:0)

您可以使用一个意图并使用 PutExtra()进行Intents,并依赖于按钮单击,您可以将数据添加到可在电子邮件客户端中使用的Extra。