2013-05-27 74 views
1

我被給予以下用於從用戶創建電子郵件表單。該應用程序收集3個字符串,但我不知道如何將提交按鈕與此電子郵件代碼結合。Android:使用onclicklistener發起電子郵件意向

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(); 
} 

回答

2

你在你的佈局創建一個Button

<ParentLayout [...]> 

    <Button [....] android:id="@+id/myButton"/> 

</ParentLayour /> 

創建Button的對象:

Button btn = (Button) findViewById(R.id.myButton); 

設置一個OnClickListner

btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Do the email stuff in here. 
      } 
     });) 
+0

在什麼地方CRE吃對象線去? (相對於活動) – coltsfan95

+0

在'onCreate()'方法中。 – Ahmad

+0

真棒謝謝你。 – coltsfan95

相關問題