2013-03-27 72 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

相關問題