0

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

好的,所以我找到了答案(我把它放在 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();
4

2 回答 2

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”

于 2013-03-27T08:29:16.770 回答
0

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

于 2013-03-27T10:03:23.657 回答