2017-01-02 45 views
4

我看着它,這是發送電子郵件的最常用方法...如何使一個按鈕提交多個文本到我的電子郵件?

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("message/rfc822"); 
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
i.putExtra(Intent.EXTRA_TEXT , "body of email"); 
try { 
    startActivity(Intent.createChooser(i, "Send mail...")); 
} catch (android.content.ActivityNotFoundException ex) { 
    Toast.makeText(MyActivity.this, "There are no email clients 
installed.",  
Toast.LENGTH_SHORT).show(); 
} 

我很困惑,這是什麼實際發送,以及如何當用戶點擊一個這種情況發生按鈕。我是否將收件人電子郵件(我)這樣?

i.putExtra(Intent.EXTRA_EMAIL , "[email protected]"); 

那麼對於郵件的主題和郵件/正文的格式是什麼?

這是我如何把用戶的輸入到電子郵件的正文? (用戶輸入多個editText框)

editText userTitle = (editText)findViewById(R.id.idOfTheEditTextBox); 
editText userDescription = (editText)findViewById(R.id.idOfTheEditTextBox); 

然後像這樣輸入嗎?

i.putExtra(Intent.EXTRA_TEXT , "userTitle", "userDescription"); 

最後,所有的烤麪包和沒有安裝電子郵件客戶端的意思是什麼?我是Android應用程序開發新手,並且正在Android Studio上製作應用程序!所有幫助非常感謝!謝謝!

+0

請參閱此鏈接https://developer.android.com/guide/components/intents-common.html#ComposeEmail –

+0

**沒有電子郵件客戶端安裝**意味着沒有電子郵件應用程序安裝在設備上像Gmail或其他。 – skydroid

回答

3

首先「吐司

吐司代碼寫入給警惕的用戶,通知沒有電子郵件 客戶端的用戶免費電話(例如,Gmail的,等)

現在的主要部分,

i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 

是的,上面這行添加了配方的電子郵件地址。

因此,要實現這一目標之類的話,你需要做以下的事情,

  1. 一個EDITTEXT(其中用戶可以把recepeints這可以傳遞給意向電子郵件地址)創建佈局。點擊一下你將啓動Intent的按鈕。
  2. 現在然後點擊按鈕時寫的代碼: -

public class MainActivity extends AppCompatActivity { 
 
    EditText etRecipentId, etSubject, etBody; 
 
    Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
    super.onCreate(savedInstanceState); 
 
    setContentView(R.layout.activity_main); 
 
    etRecipentId = (EditText) findViewById(R.id.email_id); 
 
    etSubject = (EditText) findViewById(R.id.et_subject); 
 
    etBody = (EditText) findViewById(R.id.et_body); 
 
    } 
 

 
    b1.setOnClickListener(new OnClickListener() { 
 
    public void onClick() { 
 
     Intent i = new Intent(Intent.ACTION_SEND); 
 
     i.setType("message/rfc822"); 
 
     i.putExtra(Intent.EXTRA_EMAIL, new String[] { 
 
     etRecipentId.getText().toString(); 
 
     }); 
 
     i.putExtra(Intent.EXTRA_SUBJECT, etSubject.getText().toString();); 
 
     i.putExtra(Intent.EXTRA_TEXT, etBody.getText().toString();); 
 
     try { 
 
     startActivity(Intent.createChooser(i, "Send mail...")); 
 
     } catch (android.content.ActivityNotFoundException ex) { 
 
     Toast.makeText(MyActivity.this, "There are no email clients 
 
    installed.", 
 
      Toast.LENGTH_SHORT).show(); 
 
     } 
 
    } 
 
    });

+0

好的,但我該如何發送編輯文本中的內容作爲主題,以及其他編輯文本中的內容作爲主體?我通過在主題和正文中輸入來實現它,但我希望它發送textviews/textedits中的內容。謝謝! –

+0

@EthanReinsch請檢查以上編輯的代碼,以達到您在評論中提問的內容 – Mrinmoy

+0

@EthanReinsch謝謝!快樂編碼! – Mrinmoy

0

,而不是i.putExtra(Intent.EXTRA_TEXT , "userTitle", "userDescription");使用i.putExtra(Intent.EXTRA_TEXT , userTitle+" "+userDescription);

隨着你越來越異常,檢查是否有任何電子郵件客戶端如gmail等安裝在您的手機中或不。

相關問題