2013-03-27 75 views
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 protected]" }); 
        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[] { "[email protected]" }); 
        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(); 
+0

是的,有可能。 – Nermeen 2013-03-27 07:55:51

回答

0

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

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” 为字符链

+0

对不起,我是编程新手......为什么它给我一个单词“builder”的错误?它说“建设者不能解决”。我试图让它进入AlertDialogs的构建器,但它给了我更多的错误。我已将您的代码放入按钮的onClick()方法中。谢谢一吨 – ThatGuyThere 2013-04-01 04:04:13

+0

没关系,我找到了答案。 – ThatGuyThere 2013-04-01 05:29:40

相关问题