【问题标题】:different email intents from buttons on dialog来自对话框按钮的不同电子邮件意图
【发布时间】:2013-03-27 07:53:24
【问题描述】:

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

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

【问题讨论】:

    标签: android email android-intent dialog


    【解决方案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”

    【讨论】:

    • 抱歉,我是编程新手...为什么在“builder”一词上出现错误?它说“构建器无法解决”。我试图将它变成 AlertDialogs 的构建器,但它给了我更多错误。我已将您的代码放入按钮的 onClick() 方法中。非常感谢
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      相关资源
      最近更新 更多